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

题意:补丁在修正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+;
int q[N+],f[N+],ha[N+],vis[N+],w[],pre[][],fixed[][];
int n,m,maxn,cas=;
char s1[],s2[];
void cook_the_raw(int i){
pre[i][]=pre[i][]=fixed[i][]=;
fixed[i][]=maxn;
for(int j=;j<n;j++){
if(s1[j]=='+') pre[i][]+=<<j;//pre[i][1]某位为1表示patch[i]apply条件要求该漏洞
else if(s1[j]=='-') pre[i][]+=<<j;//pre[i][0]某位为1表示patch[i]apply条件要求无该漏洞
if(s2[j]=='+') fixed[i][]+=<<j;//fixed[i][1]某位为1表示apply后得到该漏洞
else if(s2[j]=='-') fixed[i][] -=<<j;//fixed[i][0]某位为0表示apply后修复该漏洞
}
}
void bfs(){
int head,tail;
head=tail=;
q[tail++]=maxn;
vis[maxn]=true;
while(head!=tail){
int u=q[head++];
if(head==maxq) return ;
for(int i=;i<m;i++){
if(((u&pre[i][])==pre[i][])&&((u&pre[i][])==)){//满足patch[i]apply条件
int v=(u|fixed[i][])&fixed[i][];//转移到的状态
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]=;
}
}
}
}
vis[u]=;
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&n+m){
cas++;
maxn=(<<n)-;
f[maxn]=;ha[maxn]=cas;
for(int i=;i<m;i++){
scanf("%d %s %s",&w[i],s1,s2);
cook_the_raw(i);
}
bfs();
printf("Product %d\n",cas);
if(ha[]!=cas) printf("Bugs cannot be fixed.\n\n");
else printf("Fastest sequence takes %d seconds.\n\n",f[]);
}
return ;
}

Poj1482的更多相关文章

  1. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  2. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  3. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  4. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  5. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  6. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  7. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  8. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  9. ACM算法总结及刷题参考

    参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,p ...

随机推荐

  1. java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100/虚拟机调优

    JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...

  2. Solr3.6.2和Solr4.9.0经常使用配置

    tomcat 以tomcat 7为例,位置/work/apache-tomcat-7.0.55 Solr 3.6.2 基本配置 Solr 3.6.2.须要JDK 6/JDK7支持. 下载Solr 3. ...

  3. jqchart 使用的几点小技巧

    官网demo地址:http://www.jqchart.com/jquery/chart 简单示例: [javascript] $('#jqChart').jqChart({ title: 'jqCh ...

  4. js返回页面顶部

    第一次写博客,不太专业,废话不多说,直接上自己早上做的东东.有不足之处,希望指点. css: body{counter-reset: p;} p{width: 100px;margin: 20px 0 ...

  5. 运行百度语音识别官方iOS demo报错: load offline engine failed: 4001

    运行官方BDVRClientSample这个demo(ios版的),demo可以安到手机上,但是点“识别UI”那个按钮后“授权验证失败”.如果点“语音识别”那个按钮,控制台输出:2015-10-23 ...

  6. MSBuild入门(续)

    MSBuild基本概念(续) 在上一篇简单的介绍了下MSBuild中的四个基本块,每块介绍比较单薄,在这里对在大多数的项目模版生成的*.*proj文件中比较常见一些用法和概念做些补充.主要有一下几方面 ...

  7. 热烈祝贺阿尔法Go首战告捷

    这是人类的一大杰作和进步.一个国家和民族的未来在科技,靠造房子是成不了科技强国的. 当然,也要祝贺一下北上深房价突破历史高位.这也是伟大而不朽的成果.

  8. Redis 的 fields 遇到的问题

    问题描述:本地和测试环境同使用一台redis服务器,本地环境和测试环境使用 key,fileds,value 中的fileds 来区分,例如 key fields value 004920c6eba1 ...

  9. struts-config message-resources配置问题总结

    问题:我的app无法读取配置好的ApplicationResources.properties中的内容 解答:文件目录为 /webapp /WEB-INF /classes ApplicationRe ...

  10. VS2015许可证过期

    VS2015过期激活方法