UVA658-It’s not a Bug, it’s a Feature!-最短路
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
the bugs
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
思路
漏洞满满的系统打补丁
一个补丁的前字符串表示需要满足的条件,后一字符串表示对漏洞的操作
-表示需要没有/消除,+表示需要有/增添
二进制01串跑最短路
没有必要提前建图,而且麻烦,在每个状态判断就好了
AC代码
/************************************** *Source : UVA658 *Knowledge Point : SSSP *Author : CSUzick **************************************/
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <bitset>
#include <map>
#define INF 0x3f3f3f3f
#define ULL unsigned long long
#define LL long long
#define N (1<<20)
#define eps 10e-9
#define MOD 100000000
#define mem(a,n) memset(a,n,sizeof(a))
#define fread freopen("in.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
const double PI=acos(-1.0);
int n,m;
struct Patch{
int cost,need,neednot,fixed,added;
}p[110];
inline bool fit(int state,int idx){
return ((state&p[idx].need)==p[idx].need)&&((state&p[idx].neednot)==0);
}
inline int afterpatch(int state,int idx){
return ((state|p[idx].added)&(~p[idx].fixed));
}
bool inque[N];
int d[N];
char str1[22],str2[22];
void init(){
int len=1<<n;
for(int i=0;i<len;++i){
inque[i]=false;
d[i]=INF;
}
for(int i=0;i<m;++i){
scanf("%d%s%s",&p[i].cost,str1,str2);
p[i].need=p[i].neednot=p[i].fixed=p[i].added=0;
reverse(str1,str1+n);
reverse(str2,str2+n);
for(int j=0;j<n;++j){
if(str1[j]=='+'){
p[i].need|=(1<<j);
}else if(str1[j]=='-'){
p[i].neednot|=(1<<j);
}
if(str2[j]=='+'){
p[i].added|=(1<<j);
}else if(str2[j]=='-'){
p[i].fixed|=(1<<j);
}
}
}
}
void spfa(){
int start=(1<<n)-1;
queue<int> que;
que.push(start);
inque[start]=true;
d[start]=0;
while(!que.empty()){
int s=que.front();
que.pop();
inque[s]=false;
for(int i=0;i<m;++i){
if(fit(s,i)){
int as=afterpatch(s,i);
if(d[as]>d[s]+p[i].cost){
d[as]=d[s]+p[i].cost;
if(!inque[as]){
que.push(as);
inque[as]=true;
}
}
}
}
}
}
int main()
{
// fwrite;
int kase=0;
while(~scanf("%d%d",&n,&m)&&(n||m)){
init();
spfa();
printf("Product %d\n",++kase);
if(d[0]==INF){
puts("Bugs cannot be fixed.\n");
}else{
printf("Fastest sequence takes %d seconds.\n\n",d[0]);
}
}
return 0;
}