poj1483 It's not a Bug, It's a Feature!
Time Limit: 5000MS | Memory Limit: 30000K | |
Total Submissions: 1231 | Accepted: 466 |
Description
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 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
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
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int n,m,time[200],before[2][200],after[2][200],visit[2000000];
struct bugtree{int time ,bugnum;
bool operator <(const bugtree a)const //重载比较函数,为下面队作准备,也可以在外面
{
if(time>a.time)//这里是大于号,要小心这里和下面优先队列的排列顺序是有很大关系的!
return true;
else
return false ;
} };
bool bugcan(int i,bugtree f)//判断是否符合第m种的操作
{
int ignore;
ignore=f.bugnum&(~before[0][i]);//把不用考虑的全部置成0其它的不变
// printf("%d ignore\n",ignore);
if(ignore==before[1][i])//如果在不能有的位置上有1,就要返回假
return true;
return false ;
}
int bfs()
{
int i;
bugtree temp,ss;
priority_queue < bugtree > q;
memset(visit,-1,sizeof(visit));
while(!q.empty())//清空
q.pop();
temp.time=0;
temp.bugnum=(1<<n)-1;
//printf("temp %d\n",temp.bugnum);
visit[temp.bugnum]=temp.time;
q.push(temp);
while(!q.empty())
{
temp=q.top();
q.pop();
if(temp.bugnum==0)
{
return temp.time;//找到最小值,在这里判断会找到最小值
}
for(i=0;i<m;i++)
{ if(bugcan(i,temp))//满足第m条件
{ ss.time=temp.time+time[i];
ss.bugnum=(temp.bugnum|after[0][i])&after[1][i];
// printf("%d i%d ss\n",i,ss.bugnum);
if((visit[ss.bugnum]<0)||(ss.time<visit[ss.bugnum]))//未访问过,或者,时间更少,都加入队列
{
// printf(" i%d %di\n",i,ss.bugnum);
visit[ss.bugnum]=ss.time;
q.push(ss);
} }
}
}
return -1;//没有搜到
}
int main()
{
int i,j,re,tcase;
char c;tcase=1;char strtemp[200];
while(scanf("%d%d",&n,&m)!=EOF)
{ if(n==0&&m==0)
break;
for(i=0;i<m;i++ )
{
scanf("%d",&time[i]);
getchar();
before[0][i]=0;
before[1][i]=0;
after[0][i]=0;
after[1][i]=0;
for( j=0;j<n;j++)//是否能操作,如果把两者分开了,就可以全部用二进制进行操作,对于后面的搜索是非常有用的
{
c=getchar();
before[0][i]=before[0][i]<<1;//是第i个,不能用j
before[1][i]=before[1][i]<<1;//在每一个循环都要左移,为了记录每一个位置的相应的变化
if(c=='0')
{
before[0][i]=before[0][i]|1;//用1来表示是进行操作 }
else if(c=='-')//要将-和0分开,这是两种不同的操作
{
// before[1][i]=before[1][i];//用1来标记是否要这个条件
}
else if(c=='+')
{
before[1][i]=before[1][i]|1;
} }
getchar();//将中间的空格号收掉
for( j=0;j<n;j++)//改掉bug
{
c=getchar();
if(c=='+')
{
after[0][i]=after[0][i]<<1|1;//加用1或,不用管的地方用0
after[1][i]=after[1][i]<<1|1;
}
else if(c=='-')
{
after[0][i]=after[0][i]<<1;
after[1][i]=after[1][i]<<1;//减用0且,不用管的地方用1
}
else if(c=='0')
{
after[0][i]=after[0][i]<<1;
after[1][i]=(after[1][i]<<1)|1;
} }
gets(strtemp);//将最后的回车收掉 }
//printf("%d %dafter \n",after[0][0],after[1][0]);
re=bfs();
printf("Product %d\n",tcase++);
if(re!=-1)
{
printf("Fastest sequence takes %d seconds.\n\n",re);
}
else{ printf("Bugs cannot be fixed.\n\n");
} } return 0;
}
poj1483 It's not a Bug, It's a Feature!的更多相关文章
- [有意思]The IT workers of Star Wars -- That's not a bug. It's a feature
Yeah, that Artoo is kinda mouthy... ... now select, "restore to factory settings." That'll ...
- 【最短路】【位运算】It's not a Bug, it's a Feature!
[Uva658] It's not a Bug, it's a Feature! 题目略 UVA658 Problem PDF上有 试题分析: 本题可以看到:有<=20个潜在的BUG,那 ...
- UVA 658 It's not a Bug, it's a Feature! (最短路,经典)
题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了: ...
- UVa 658 (Dijkstra) It's not a Bug, it's a Feature!
题意: 有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间. 求修复所有BUG的最短时间. 分析: 可以用n个二进制位表示这 ...
- UVA 658 It's not a Bug, it's a Feature!
这个题目巧妙之处在于用二进制的每个位1,0分别表示bug的有无,以及实施补丁对相应bug的要求以及实施后的对bug的影响. 软件bug的状态:1表示相应bug仍然存在,0表示已经修复.这样可以将软件的 ...
- UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 658 It's not a Bug, it's a Feature! (状态压缩+Dijstra)
题意:首先给出n和m,表示有n个bug和m个补丁.一开始存在n个bug,用1表示一个bug存在0表示不存在,所以一开始就是n个1,我们的目的是要消除所有的bug, 所以目标状态就是n个0.对于每个补丁 ...
- UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...
- 洛谷 题解 UVA658 【这不是bug,而是特性 It's not a Bug, it's a Feature!】
[题意] 补丁在修正\(BUG\)时,有时也会引入新的\(BUG\),假定有\(n(n<=20)\)个潜在\(BUG\),和\(m(m<=100)\)个补丁,每个补丁用两个长度为\(n\) ...
随机推荐
- C++并发编程学习笔记<1> 入门
入门 多线程C++程序是什么样子的? 它看上去和其它全部C++程序一样,一般是变量.类以及函数的组合. 唯一真正的差别在于某些函数能够并发执行, 当然.为了并发地执行函数,必须使用特定的函数以及对象来 ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- 你要知道的C与C++的区别
原文:你要知道的C与C++的区别 如果要说C和C++的区别的话,可能可以列出很多方面出来,但是有许多方面的区别是我们学完这两门语言之后就可以 很好的理解和区分的,比如C是面向过程的一门编程语言,C++ ...
- thinkphp 支付宝错误 Class 'Think' not found
Class 'Think' not found D:\www\DonatePlatform\ThinkPHP\Extend\Vendor\alipay\lib\alipay_submit.class. ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- SQL点滴24—监测表的变化
原文:SQL点滴24-监测表的变化(转载) 在网上看到一篇关于监测表中的插入,更新,删除的方法,使用触发器实现的,很有价值. 地址:http://www.dbaunion.com/u/livecoac ...
- jQuery EasyUI API - Base - Draggable [原创汉化官方API]
最近在学习jQuery EasyUI,发现中文的文档好少,部分文档不错但它是鸟语的,为了大家也为了自己学习吧,汉化做一下笔记. 有没有说清楚的,或者翻译不正确的地方还请大家谅解指出.. 由于工作时间原 ...
- Asterisk 未来之路3.0_0001
原文:Asterisk 未来之路3.0_0001 第一章:电信技术革命 刚开始他们忽视你,然后他们嘲笑你,然后他们向你挑战,最后你赢了 ---Mahatma Ganhdi 在5年前,我最初规划写一本关 ...
- jquery背景动画插件使用
在网页制作动画特效的时候,有时候想通过背景插入图片,然后通过控制背景显示的位置来实现一些动画效果,这样就不用使用绝对定位控制left和top来实现动画效果!但是jquery本身的动画函数是不支持背景动 ...
- win7 64位下如何安装配置mysql-5.7.7-rc-winx64
距离上次安装MySQL已经过去好久了.步骤这些,有可能会忘记.简单记录一下吧.(参考了一些网络上的博客.) 1.mysql-5.7.5-m15-winx64.zip下载 官方网站下载地址: http: ...