Poj1482

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

Time Limit: 5000MS   Memory Limit: 30000K
Total Submissions: 1428   Accepted: 557

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

Southwestern European Regional Contest 1998

题意:补丁在修正bug时,有时也会引入新的bug。假定有n个潜在的bug m个补丁,每个补丁用两个长度为n的字符串表示,其中字符串的每个位置表示一个bug,第一个串表示打补丁之前的状态(‘-‘表示该bug必须不存在,’+‘表示必须存在,0表示无所谓,第二个串表示打补丁之后的状态(-‘表示不存在,’+‘表示存在,0表示不变)。每个补丁都有一个执行时间,你的任务使用最少的时间把一个bug都存在的软件通过打补丁的方式变得没有bug。一个补丁可以打多次。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N (1<<20)
const int maxq=N+10;
int q[N+15],f[N+10],ha[N+10],vis[N+10],w[110],pre[110][2],fixed[110][2];
int n,m,maxn,cas=0;
char s1[30],s2[30];
void cook_the_raw(int i){
    pre[i][0]=pre[i][1]=fixed[i][1]=0;
    fixed[i][0]=maxn;
    for(int j=0;j<n;j++){
        if(s1[j]==‘+‘) pre[i][1]+=1<<j;//pre[i][1]某位为1表示patch[i]apply条件要求该漏洞
        else if(s1[j]==‘-‘) pre[i][0]+=1<<j;//pre[i][0]某位为1表示patch[i]apply条件要求无该漏洞
        if(s2[j]==‘+‘) fixed[i][1]+=1<<j;//fixed[i][1]某位为1表示apply后得到该漏洞
        else if(s2[j]==‘-‘) fixed[i][0] -=1<<j;//fixed[i][0]某位为0表示apply后修复该漏洞
    }
}
void bfs(){
    int head,tail;
    head=tail=0;
    q[tail++]=maxn;
    vis[maxn]=true;
    while(head!=tail){
        int u=q[head++];
        if(head==maxq) return ;
        for(int i=0;i<m;i++){
            if(((u&pre[i][1])==pre[i][1])&&((u&pre[i][0])==0)){//满足patch[i]apply条件
                int v=(u|fixed[i][1])&fixed[i][0];//转移到的状态
                if(ha[v]!=cas||f[v]>f[u]+w[i]){
                    f[v]=f[u]+w[i];ha[v]=cas;
                    if(!vis[v]){
                        q[tail++]=v;
                        if(tail==maxq) return ;
                        vis[v]=1;
                    }
                }
            }
        }
        vis[u]=0;
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)&&n+m){
        cas++;
        maxn=(1<<n)-1;
        f[maxn]=0;ha[maxn]=cas;
        for(int i=0;i<m;i++){
            scanf("%d %s %s",&w[i],s1,s2);
            cook_the_raw(i);
        }
        bfs();
        printf("Product %d\n",cas);
        if(ha[0]!=cas) printf("Bugs cannot be fixed.\n\n");
        else printf("Fastest sequence takes %d seconds.\n\n",f[0]);
    }
    return 0;
}
时间: 2024-09-30 15:37:55

Poj1482的相关文章

poj1482(隐式图求最短路)

题目链接 题意:补丁在修正bug时,有时也会引入新的bug.假定有n个潜在的bug m个补丁,每个补丁用两个长度为n的字符串表示,其中字符串的每个位置表示一个bug,第一个串表示打补丁之前的状态('-'表示该bug必须不存在,'+'表示必须存在,0表示无所谓,第二个串表示打补丁之后的状态(-'表示不存在,'+'表示存在,0表示不变).每个补丁都有一个执行时间,你的任务使用最少的时间把一个bug都存在的软件通过打补丁的方式变得没有bug.一个补丁可以打多次. 解法:状压表示每个补丁的存在与否.隐式

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法

ACM算法总结及刷题参考

参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一.基本算法: (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递推.     (5)构造法.(po

POJ题目推荐(转载)

POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要求每个题目都要写总结,养成良好的习惯.6.这个列表的目的在于让大家对各个方面的算法有个了解,也许要求有些苛刻,教条,请大家谅

POJ题目分类

初期: 一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,he

嗷嗷嗷,kuangbin大大博客上拉的题

正在学(learning),未学(waiting),已学(cut  vovering) 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.  

转:转一个搞ACM需要的掌握的算法. .

要注意,ACM的竞赛性强,因此自己应该和自己的实际应用联系起来.  适合自己的才是好的,有的人不适合搞算法,喜欢系统架构,因此不要看到别人什么就眼红,  发挥自己的长处,这才是重要的. 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,  因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打  出来.  1.最短路(Floyd.Dijstra,BellmanFord)  2.最小生成树(先写个prim,kruscal要用并查集,不好写)

算法初学者指南

摘自网络,对于这个训练计划,我只能膜拜,~ 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码, 因为太常用,所以要练到写时不用想,10-15 分钟内打完,甚至关掉显示器都可以把程序打 出来. 1.最短路(Floyd.Dijstra,BellmanFord) 2. 最小生成树(先写个prim,kruscal要用并查集,不好写) 3.大数(高精度)加减乘除 4.二分查找. (代码可在五行以内) 5.叉乘.判线段相交.然后写个凸包. 6.BFS.DFS,同时熟练hash表(

POJ分类

初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj3295) (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法: (1)图的深度优先遍历和广度优先遍历. (2)最短路径算法(dijkstra,bellman-ford,floyd,heap+dijkstra) (poj1860,poj3259,p