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

题解:

这折磨人的题目…补丁在修正BUG时,有时也会引入新的BUG,假定有n(n<=20)个潜在BUG,和m(m<=100)个补丁,每个补丁用两个长度为n的字符串表示,其中字符串的每个位置表示一个bug,第一个串表示打补丁之前的状态(“-”表示该BUG必须不存在,“+”表示该补丁必须存在,0表示无所谓),第二串表示打补丁之后的状态(”-“表示不存在,“+”表示存在,0表示不变)。每个补丁有一定的执行时间,你的任务是用最小的时间把所有BUG都存在的软件变得没有BUG。

在任意时刻,每个bug可能存在也可能不存在,所以可以用一个n位二进制表示当前状态。把状态看成结点,状态转移看成边,转化为图论的最短路。同时当需要得到某个结点u出发的所有边时,直接枚举所有m个补丁

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
const int maxn = 110;
const int INF = 0x3f3f3f3f;
#define LL long long
int vis[(1<<20)+10];
int d[(1<<20)+10];
int s[2][maxn];
int t[2][maxn];
string s1,s2;
int n,m,w[maxn];
void dij()
{
    int Max=(1<<n)-1;
    queue<int>q;
    for (int i = 0;i<Max;i++)
    {
        vis[i]=0;
        d[i]=INF;
    }
    d[Max]=0;
    q.push(Max);
    int u,v;
    while (!q.empty())
    {
        u=q.front();
        vis[u]=0;
        q.pop();
        for (int i = 0;i<m;i++)
        {
            if ((u | s[1][i])== u && (u & (s[0][i]))==u)
            {
                v=u;
                v = v | t[1][i];
                v = v & t[0][i];
                if (d[u]+w[i] <d[v])
                {
                    d[v]=d[u]+w[i];
                    if (!vis[v])
                    {
                        q.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
    }
    if (d[0]==INF)
        printf("Bugs cannot be fixed.\n");
    else
        printf("Fastest sequence takes %d seconds.\n",d[0]);
}
int cas=1,T;

int main()
{
    while (scanf("%d%d",&n,&m)!=EOF && n)
    {
        memset(s,0,sizeof(s));
        memset(t,0,sizeof(t));
        for (int i = 0;i<m;i++)
        {
            cin >> w[i] >> s1 >> s2;
            for (int j = 0;j<n;j++)
            {
                if (s1[j]=='+')
                    s[1][i]+=(1<<j);
                if (s1[j]!='-')
                    s[0][i]+=(1<<j);
                if (s2[j]=='+')
                    t[1][i]+=(1<<j);
                if (s2[j]!='-')
                    t[0][i]+=(1<<j);
            }
        }
        printf("Product %d\n",cas++);
        dij();
        printf("\n");
    }
    return 0;
}

题目

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 B+iB have to be present in the software, and the bugs BiB must be absent (of course B+iBi= holds). The patch then fixes the bugs FiB (if they have been present) and introduces the new bugs F+iB (where, again, FiBi= ).
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 1n20 and 1m100 . 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 a0” 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.

你可能感兴趣的:(UVA658 It's not a Bug, it's a Feature!)