COGS439. [Network flow 24] software patches

Source: Internet
Author: User

"Problem description" for a software company, after the release of a new software, it can be said that the work has been completed. But in fact, many software companies release a new product, often send patches, modify the original product errors (of course, some patches are charged). such as a micro-hard company is such a software company. This summer, after a new word-processing software was released, they have written many patches now. Just this weekend, they solved a big problem in the software with the newly written patch. While each patch modifies some errors in the software, it may cause some of the original errors in the software to re-attack. This occurs because, when an error is modified, the patch takes advantage of the special behavior that is contracted in the program, causing the error to be re-generated. Micro-hard companies found in their software a total of n error b={bl,b2,...,bn), now they sent a total of M patch p1,p2,...,pm. If you want to apply the PI number patch in the software, error b+i ≤b must be present in the software, and error b-i≤b must not exist (obviously, B+i∩b-i is an empty set). The patch then corrects the error f-i≤b (if the error exists) and generates a new error f+i≤b (again, F+i∩f-i is an empty set). Now, there is only one problem for micro-hard companies. They give an original version of the software, the software contains all the errors in B, and then in a certain order in the software to apply the patch (when applying a patch, the software must meet the application conditions of the patch, and run the program will take a certain amount of time). Ask how to fix all errors in the software as quickly as possible (that is, the shortest total time to fix all bugs)?  the "input format" data is stored in the text file "Bugs.in" in the current directory. The first line of the file contains two integers n and m, respectively, indicating the number of errors in the software and the number of patches sent. where N and M meet the condition: 1≤n≤20,1≤m≤100. The next M-line (that is, line 2nd to m+1) describes the case of m patches sequentially, and line I describes patch i-1. Each row contains an integer that represents the time required to apply the patch in the software, in seconds, and a string of two n characters (separated by a space in the middle). , the first string describes the condition of applying the patch (I-1), which indicates whether an error should exist or should not exist in the software. The first character of the string, if "+", indicates that there must be a bi-number error in the software, and if "-" means that error bi cannot exist in the software, or "0", it means that the error bi exists or does not exist (that is, there is no effect on applying the patch). The second string describes the effect of applying the patch (I-1), which is to apply a patch, which errors were modified and what new errors were created. The first character of the string, if "+", indicates that a new error is generated for BI; if "-" indicates that the error bi has been modified, and if it is "0", it means that the error bi is unchanged (that is, the original exists, it still exists; it does not exist).   Output format answer output is in the text file "Bugs.out" in the current directory. Please find an optimal order of application patches, modify all errors in the software, and use the least amount of time. Note that each patch can be applied multiple times. If such a sequence exists, the total amount of time (in seconds) to apply the patch in the first line of the output file, and if such a sequence is not found, output-1 in the first line of the output file.   Sample Input Output sample input (bugs.in): 3 31 00-1 00-0-+2 0---+ + sample output (Bugs.out): 8

This problem is not actually a network flow.

State compression indicates the presence status of each bug, and the shortest path from 1111111 to 0000000 is obtained.

No need to explicitly build edges (nor build)

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <algorithm>5#include <queue>6 #defineLL Long Long7 using namespacestd;8 Const intinf=1e9;9 Const intmxm=1<< +;Ten Const intmxn= the; One intn,m; A intDIS[MXM]; - structpk{ -     intQ,c;//must exist error, cannot exist error the     intK1,K2;//Repair Effect -     intW; - }A[MXN]; - BOOLINQ[MXM]; + BOOLPdintXintY) {//determine if patches can be applied -     if((X|A[Y].Q)!=x)return 0; +     if((X&AMP;A[Y].C))return 0; A     return 1; at } - voidSPFA () { -memset (DIS,0x3f,sizeofdis); -queue<int>Q; - Q.push (n); -dis[n]=0; in      while(!Q.empty ()) { -         intU=q.front (); Q.pop (); inq[u]=0; to          for(intI=1; i<=m;i++){ +             if(PD (U,i)) { -                 intv= (u^ (U&AMP;A[I].K2)) |A[i].k1; the                 if(dis[v]>dis[u]+A[I].W) { *dis[v]=dis[u]+A[I].W; $                     if(!Inq[v]) {Panax Notoginsenginq[v]=1; - Q.push (v); the                     } +                 } A             } the         } +     } -     return; $ } $ intMain () { -Freopen ("bugs.in","R", stdin); -Freopen ("Bugs.out","W", stdout); the     inti,j; -scanf"%d%d",&n,&m);Wuyi     Chars[ -]; the      for(i=1; i<=m;i++){ -scanf"%d%s",&a[i].w,s); Wu          for(j=0; j<n;j++){ -             if(s[j]=='+') a[i].q|= (1<<j); About             if(s[j]=='-') a[i].c|= (1<<j); $         } -scanf"%s", s); -          for(j=0; j<n;j++){ -             if(s[j]=='+') a[i].k1|= (1<<j); A             if(s[j]=='-') a[i].k2|= (1<<j); +         } the     } -N= (1<<n)-1; $ SPFA (); the     if(dis[0]!=0x3f3f3f3f) printf ("%d\n", dis[0]); the     Elseprintf"-1\n"); the     return 0; the}

COGS439. [Network flow 24] software patches

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.