POJ 1482 HASH+BFS


It's not a Bug, It's a Feature!

Time Limit: 5000MS   Memory Limit: 30000K
Total Submissions: 1141   Accepted: 438

Description


It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right? Or a CD-player that plays only CDs with country music on them? Probably not. But for software systems it seems to be acceptable if they do not perform as they should do. In fact, many software companies have adopted the habit of sending out patches to fix bugs every few weeks after a new product is released (and even charging money for the patches). Tinyware Inc. is one of those companies. After releasing a new word processing software this summer, they have been producing patches ever since. Only this weekend they have realized a big problem with the patches they released. While all patches fix some bugs, they often rely on other bugs to be present to be installed. This happens because to fix one bug, the patches exploit the special behavior of the program due to another bug.
More formally, the situation looks like this. Tinyware has found a total of n bugs B = {b1, b2, ..., bn} in their software. And they have released m patches p1, p2, ..., pm. To apply patch pi to the software, the bugs Bi+ in B have to be present in the software, and the bugs Bi- in B must be absent (of course Bi+ ∩ Bi- = Φ). The patch then fixes the bugs Fi- in B (if they have been present) and introduces the new bugs Fi+ in B (where, again, Fi+ ∩ Fi- = Φ).
Tinyware's problem is a simple one. Given the original version of their software, which contains all the bugs in B, it is possible to apply a sequence of patches to the software which results in a bug- free version of the software? And if so, assuming that every patch takes a certain time to apply, how long does the fastest sequence take?

Input


The input contains several product descriptions. Each description starts with a line containing two integers n and m, the number of bugs and patches, respectively. These values satisfy 1 <= n <= 20 and 1 <= m <= 100. This is followed by m lines describing the m patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, and two strings of n characters each.
The first of these strings describes the bugs that have to be present or absent before the patch can be applied. The i-th position of that string is a ``+'' if bug bi has to be present, a ``-'' if bug bi has to be absent, and a `` 0'' if it doesn't matter whether the bug is present or not.
The second string describes which bugs are fixed and introduced by the patch. The i-th position of that string is a ``+'' if bug bi is introduced by the patch, a ``-'' if bug bi is removed by the patch (if it was present), and a ``0'' if bug bi is not affected by the patch (if it was present before, it still is, if it wasn't, is still isn't).
The input is terminated by a description starting with n = m = 0. This test case should not be processed.

Output


For each product description first output the number of the product. Then output whether there is a sequence of patches that removes all bugs from a product that has all n bugs. Note that in such a sequence a patch may be used multiple times. If there is such a sequence, output the time taken by the fastest sequence in the format shown in the sample output. If there is no such sequence, output ``Bugs cannot be fixed.''.
Print a blank line after each test case.

Sample Input


3 3
1 000 00-
1 00- 0-+
2 0-- -++
4 1
7 0-0+ ----
0 0

Sample Output


Product 1
Fastest sequence takes 8 seconds.

Product 2
Bugs cannot be fixed.

Source







1
#include <algorithm> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 8 class Patch 9 { 10 public: 11 int t; 12 int bug_1; // 0变0 13 int bug_2; // 0变1 14 int result_1; // 0变0 15 int result_2; // 0变1 16 void set(const int & num,const string & s1,const string & s2) 17 { 18 t=num; 19 bug_1=bug_2=0; 20 result_1=result_2=0; 21 setBug_1(s1); 22 setBug_2(s1); 23 setResult_1(s2); 24 setResult_2(s2); 25 } 26 void setBug_1(const string & str) 27 { 28 for(unsigned int j=0;j<str.size();j++) 29 { 30 bug_1*=2; 31 if(str[j]=='+') 32 { 33 bug_1+=1; 34 } 35 } 36 } 37 void setBug_2(const string & str) 38 { 39 for(unsigned int j=0;j<str.size();j++) 40 { 41 bug_2*=2; 42 if(str[j]!='-') 43 { 44 bug_2+=1; 45 } 46 } 47 } 48 int getResult(const int & cur) const 49 { 50 return (cur&result_2)|result_1; 51 } 52 void setResult_1(const string & str) 53 { 54 for(unsigned int j=0;j<str.size();j++) 55 { 56 result_1*=2; 57 if(str[j]=='+') 58 { 59 result_1+=1; 60 } 61 } 62 } 63 void setResult_2(const string & str) 64 { 65 for(unsigned int j=0;j<str.size();j++) 66 { 67 result_2*=2; 68 if(str[j]!='-') 69 { 70 result_2+=1; 71 } 72 } 73 } 74 bool isMatch(const int & n,const int & cur) 75 { 76 if(((((1<<n)-1)-(bug_2-bug_1))&cur)==bug_1) 77 { 78 return true; 79 } 80 return false; 81 } 82 bool operator()(const Patch & x,const Patch & y) const 83 { 84 return x.t<y.t?1:0; 85 } 86 }; 87 88 int main() 89 { 90 int n,m,Case=0; 91 unsigned int i; 92 while(cin>>n>>m) 93 { 94 if(!n&&!m) break; 95 int k,x,cur=(1<<n)-1; 96 string s1,s2; 97 vector<Patch> v(m); 98 vector<int> mark(1<<n,0); 99 vector<int> result(1<<n,0); 100 mark[cur]=1; 101 for(k=0;k<m;k++) 102 { 103 cin>>x>>s1>>s2; 104 v[k].set(x,s1,s2); 105 } 106 sort(v.begin(),v.end(),Patch()); 107 queue<int> q; 108 q.push(cur); 109 bool flag=false; 110 int minResult=-1; 111 while(!q.empty()) 112 { 113 int xv=q.front(); 114 q.pop(); 115 for(i=0;i<v.size();i++) 116 { 117 if(v[i].isMatch(n,xv)) 118 { 119 int xk=v[i].getResult(xv); 120 if(xk==0) 121 { 122 if(minResult==-1) minResult=result[xv]+v[i].t; 123 else minResult=min(minResult,result[xv]+v[i].t); 124 flag=true; 125 continue; 126 } 127 if(mark[xk]!=0&&result[xk]<=result[xv]+v[i].t) continue; 128 mark[xk]=1; 129 result[xk]=result[xv]+v[i].t; 130 q.push(xk); 131 } 132 } 133 } 134 cout<<"Product "<<++Case<<endl; 135 if(flag) 136 { 137 cout<<"Fastest sequence takes "<<minResult<<" seconds."<<endl; 138 } 139 else 140 { 141 cout<<"Bugs cannot be fixed."<<endl; 142 } 143 cout<<endl; 144 } 145 return 0; 146 }

 

posted @ 2013-03-04 17:55  zyh123101  阅读(174)  评论(0编辑  收藏  举报