Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

id=22169" class="login ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">SubmitStatus

Description

It is a curious fact that consumers buying a new software product generally donot 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 in their software. And they have releasedm patches
. To apply patchpi to the software, the bugs
have to be present in the software, and the bugs
must be absent (of course
holds). The patch then fixes the bugs (if they have been present) and introduces the new bugs
(where, again,).

Tinyware's problem is a simple one. Given the original version of their software, which contains all the bugs inB, 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 integersn and
m, the number of bugs and patches, respectively. These values satisfy and.
This is followed bym 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 ofn characters each.

The first of these strings describes the bugs that have to be present or absent before the patch can be applied. Thei-th position of that string is a ``+'' if bug
bi has to be present, a ``-'' if bugbi 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. Thei-th position of that string is a ``+'' if bug
bi is introduced by the patch, a ``-'' if bugbi is removed by the patch (if it was present), and a ``0'' if bugbi 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.

id=22169">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 alln 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.

题意:补丁在修补bug时,有时候会引入新的bug。

假定有n(n<=20)个潜在bug和m(m<=100)个补丁,每一个补丁用两个长度为n的字符串表示,当中字符串的每一个位置表示一个bug。

第一个串表示打补丁前的状态(“-”表示该bug必须不存在,“+”表示必须存在。“0”表示无所谓)。第二个串表示打补丁后的状态(“-”表示不存在,“+”表示存在,“0”表示不变)。

每一个补丁都有一个运行时间,你的任务是用最少的时间把一个全部bug都存在的软件通过打补丁的方式变得没有bug。一个补丁能够打多次。

分析:最短路。把状态看做结点,状态转移看做边。然后用spfa求解就可以。

结点数非常多。多达2^n个。而且非常多状态碰不到。所以不须要先把图存好。每次取出一个结点,直接枚举m个补丁。看是否可以打得上。

注:状态能够用二进制存。题目中巧妙运用位运算。

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22169

代码清单:

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxs = 20 + 5;
const int maxm = 100 + 5;
const int maxn = (1<<20) + 5;
const int max_dis = 0x7f7f7f7f; struct Patch{
int dis;
char s1[maxs];
char s2[maxs];
}patch[maxm]; int n,m,cases;
int dist[maxn];
bool vis[maxn]; void input(){
for(int i=1;i<=m;i++){
scanf("%d%s%s",&patch[i].dis,patch[i].s1,patch[i].s2);
}
} //推断该补丁是否可用
bool useful(int u,int idx){
for(int i=n-1;i>=0;i--){
int idd=(u>>(n-i-1))&1;
if(patch[idx].s1[i]=='-'&&idd==1)
return false;
if(patch[idx].s1[i]=='+'&&idd==0)
return false;
}return true;
} //得到打完补丁后的状态
int get_v(int u,int idx){
int vv=0;
for(int i=n-1;i>=0;i--){
int idd=(u>>(n-i-1))&1;
if(patch[idx].s2[i]=='0'){
if(idd==1) vv+=(1<<(n-i-1));
}
if(patch[idx].s2[i]=='+'){
vv+=(1<<(n-i-1));
}
}return vv;
} int spfa(){
int s=(1<<n)-1;
memset(vis,false,sizeof(vis));
memset(dist,max_dis,sizeof(dist)); //cout<<max_dis<<" "<<dist[0]<<endl;
dist[s]=0; vis[s]=true;
queue<int>que;
while(!que.empty()) que.pop();
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop(); vis[u]=false;
for(int i=1;i<=m;i++){
if(useful(u,i)){
int v=get_v(u,i);
if(dist[v]>dist[u]+patch[i].dis){
dist[v]=dist[u]+patch[i].dis;
if(!vis[v]){
vis[v]=true;
que.push(v);
}
}
}
}
}
} void solve(){
spfa();
printf("Product %d\n",++cases);
if(dist[0]==max_dis)
printf("Bugs cannot be fixed.\n\n");
else
printf("Fastest sequence takes %d seconds.\n\n",dist[0]);
} int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(!n&&!m) break;
input();
solve();
}return 0;
}

uva_658_It&#39;s not a Bug, it&#39;s a Feature!(最短路)的更多相关文章

  1. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  2. It&#39;s not a Bug, It&#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

    Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Su ...

  3. 错误号码2003 Can&#39;t connect to MySQL server &#39;localhost&#39; (0)

    错误描写叙述 错误原因 近期,我一直都能够用SQLyog连接本地数据库,可是近几天却无法连接:而且一直都报上述错误,我查阅了非常多资料,发现有非常多中说法 总结一下 第一,MySQL中的my.ini出 ...

  4. 退役笔记一#MySQL = lambda sql : sql + &#39; Source Code 4 Explain Plan &#39;

    Mysql 查询运行过程 大致分为4个阶段吧: 语法分析(sql_parse.cc<词法分析, 语法分析, 语义检查 >) >>sql_resolver.cc # JOIN.p ...

  5. Error creating bean with name &#39;com.you.user.dao.StudentDaoTest&#39;: Injection of autowired dependencies

    1.错误叙述性说明 七月 13, 2014 6:37:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadB ...

  6. error: &#39;Can&#39;t connect to local MySQL server through socket &#39;/var/lib/mysql/mysql.sock&#39; (2)&#39;

    [root@luozhonghua ~]#   /usr/bin/mysqladmin -u root password 'aaaaaa' /usr/bin/mysqladmin: connect t ...

  7. type &#39;simple Class&#39; does not conform to protocol &#39;Example Protocol&#39;错误

    在看swift教程中"接口和扩展"这小部分. 在编写时提示"type 'simple Class' does not conform to protocol 'Examp ...

  8. UVA 658 It's not a Bug, it's a Feature! (最短路,经典)

    题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了: ...

  9. 第四节 Code 39 码 / 三九码

    39码是西元1974年发展出来的条码系统,是一种可供使用者双向扫瞄的分散式条码,也就是说相临两资料码之间,必须包含一个不具任何意义的空白(或细白,其逻辑值为0),且其具有支援文数字的能力,故应用较一般 ...

随机推荐

  1. Alcatraz:管理Xcode插件

    简单介绍 Alcatraz是一个帮你管理Xcode插件.模版以及颜色配置的工具. 它能够直接集成到Xcode的图形界面中,让你感觉就像在使用Xcode自带的功能一样. 安装和删除 使用例如以下的命令行 ...

  2. Oracle HR 例子用户的建立 10g,11g均可

    Oracle HR 例子用户的建立 10g,11g均可 先将附件(见文章尾部)上的 10 个 .sql 文件放入这个路径中 : $ORACLE_HOME/demo/schema/human_resou ...

  3. jquery10 闭包示例

    o = { a:1, o:{ b:2, f : function(){ alert(o.a); alert(o.b);//undefined } } } o.o.f(); o = { a:7, o : ...

  4. 用C#调用Lua脚本

    用C#调用Lua脚本 一.引言 学习Redis也有一段时间了,感触还是颇多的,但是自己很清楚,路还很长,还要继续.上一篇文章简要的介绍了如何在Linux环境下安装Lua,并介绍了在Linux环境下如何 ...

  5. js -- canvas img 封装

    鼠标   1.操作canvas 中的 img. 右键放大缩小,左键移动img. 2.拖动input type= range  改变图片的透明度 html 代码 <!DOCTYPE html> ...

  6. vue-cli 搭建

    一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v  检测你是否安装了npm和版本情况.出现版 ...

  7. 37.cgi网页交互

    1.Apache的安装 地址:链接:https://pan.baidu.com/s/1kWdSWwZ 密码:nuqo 2.在相应路径下写html如图所示 new.html代码: <html> ...

  8. 使用Java语言开发微信公众平台(六)——获取access_token

             在前四期的文章中,我们分别学习了“环境搭建与开发接入”.“文本消息的接收与响应”.“被关注回复与关键词回复”.“图文消息的发送与响应”等环节.那么,从本篇博文开始,我们将进去更高级的 ...

  9. 【习题 8-3 UVA - 12545】Bits Equalizer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果1的个数第一个串比第2个串多. 那么就无解. 否则. 找几个位置去凑1 优先找'?'然后才是0的位置 剩余的全都用swap操作就 ...

  10. 【2017 Multi-University Training Contest - Team 2】 Is Derek lying?

    [Link]: [Description] 两个人都做了完全一样的n道选择题,每道题都只有'A','B','C' 三个选项,,每道题答对的话得1分,答错不得分也不扣分,告诉你两个人全部n道题各自选的是 ...