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
= f b 1 ; b 2 ; : : : ; b n g in their software. And they have released m patches p 1 ; p 2 ; : : : ; p m . To apply patch p i to the
software, the bugs B
+ i B have to be present in the software, and the bugs B
二进制压缩保存状态和补丁,然后用类似堆优化dijkstra/A*+判重【我也不知道算啥】的方法扩展。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n,m,w[110],dis[5000010];
unsigned int a[110],b[110],c[110],d[110];
void init()
{
int i,j;
char s[110];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
for (i=1;i<=m;i++)
{
scanf("%d",&w[i]);
scanf("%s",s);
for (j=0;j<n;j++)
if (s[j]!='0')
{
a[i]^=1<<j;
if (s[j]=='+')
b[i]^=1<<j;
}
scanf("%s",s);
for (j=0;j<n;j++)
if (s[j]!='0')
{
if (s[j]=='+')
d[i]^=1<<j;
}
else
c[i]^=1<<j;
}
}
int solve()
{
int j;
unsigned int u,v,x;
pair<int,int> tem;
priority_queue<pair<int,int> >q;
q.push(make_pair(0,(1<<n)-1));
memset(dis,0x3f,sizeof(dis));
dis[(1<<n)-1]=0;
while (!q.empty())
{
tem=q.top();
u=tem.second;
x=-tem.first;
q.pop();
if (u==0) return x;
for (j=1;j<=m;j++)
if (((u&a[j])^b[j])==0)
{
v=(u&c[j])|d[j];
if (x+w[j]<dis[v])
{
dis[v]=x+w[j];
q.push(make_pair(-dis[v],v));
}
}
}
return -1;
}
int main()
{
int K=0,x;
while (scanf("%d%d",&n,&m)&&n)
{
printf("Product %d\n",++K);
init();
if ((x=solve())!=-1) printf("Fastest sequence takes %d seconds.\n",x);
else printf("Bugs cannot be fixed.\n");
printf("\n");
}
}