[题解]UVA658 It's not a Bug, it's a Feature!
链接:http://vjudge.net/problem/viewProblem.action?id=22169
描述:有n个漏洞,m个修复漏洞的方法,每种方法耗时不一样,求修复漏洞的最短时间。每种方法的使用对当前漏洞分布的状态有要求,符合要求才能修复,并有可能引入新漏洞。
思路:单源点最短路
这道题卡了我很久,因为不知道怎么表示状态。最开始受到'0'的影响,觉得必须要三进制,于是写得很麻烦还错了。之后觉得可以用二进制分别存储'+'和'-'的状态。我定义两个数组,Condition[i][0]表示第i个方法'+'要满足的条件,Condition[i][1]表示第i个方法'-'要满足的条件;Operate[i][0]和Operate[i][1]同理表示修复手段。
判断当前状态Cur是否可以用第i个方法:(可以自己举个例子推一下)
Cur&Condition[i][0])==Condition[i][0]
((~Cur)&Condition[i][1])==Condition[i][1]
用第i个方法修复漏洞:
Cur|=Operate[i][0];
Cur&=(~Operate[i][1]);
然后的问题就是,状态量太多了,或者说是无效的状态量太多了,怎么解决存储问题呢?这个时候我们就考虑不存边,在搜索的时候按照判断结果走,相当于是动态找状态。然后一个bfs就解决问题了。
我的实现:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 using namespace std;
6 #define MaxM 120
7 #define MaxN 30
8 int Cost[MaxM],Condition[MaxM][5],Operate[MaxM][5];
9 char s1[MaxN],s2[MaxN];
10 bool vis[(1<<20)+20];
11 int n,m,ans;
12 bool Sol;
13 typedef pair<int, int> p;
14 struct cmp
15 {
16 bool operator()(p a,p b)
17 {
18 return a.first>b.first;
19 }
20 };
21 priority_queue <p, vector<p>, cmp> q;
22 inline void Clean()
23 {
24 memset(Condition,0,sizeof(Condition));
25 memset(Operate,0,sizeof(Operate));
26 Sol=false;
27 memset(vis,false,sizeof(vis));
28 while(!q.empty())
29 q.pop();
30 }
31 void Bfs()
32 {
33 q.push(make_pair(0,(1<<n)-1));
34 p u;
35 int i,fi,Cur;
36 while(!q.empty())
37 {
38 u=q.top();q.pop();
39 fi=u.first;Cur=u.second;
40 if(!Cur)
41 {
42 Sol=true;
43 ans=fi;
44 return;
45 }
46 if(vis[Cur])
47 continue;
48 vis[Cur]=true;
49 for(i=1;i<=m;++i)
50 {
51 Cur=u.second;
52 if((Cur&Condition[i][0])==Condition[i][0]&&((~Cur)&Condition[i][1])==Condition[i][1])
53 {
54 Cur|=Operate[i][0];
55 Cur&=(~Operate[i][1]);
56 q.push(make_pair(fi+Cost[i],Cur));
57 }
58 }
59 }
60 }
61 int main()
62 {
63 int i,j,t;
64 for(t=1;;t++)
65 {
66 Clean();
67 scanf("%d%d",&n,&m);
68 if(n==0&&m==0)
69 break;
70 for(i=1;i<=m;++i)
71 {
72 scanf("%d%s%s",&Cost[i],s1,s2);
73 for(j=0;j<n;++j)
74 {
75 if(s1[j]=='+')
76 Condition[i][0]|=(1<<j);
77 else if(s1[j]=='-')
78 Condition[i][1]|=(1<<j);
79 }
80 for(j=0;j<n;++j)
81 {
82 if(s2[j]=='+')
83 Operate[i][0]|=(1<<j);
84 else if(s2[j]=='-')
85 Operate[i][1]|=(1<<j);
86 }
87 }
88 Bfs();
89 printf("Product %d\n",t);
90 if(Sol)
91 printf("Fastest sequence takes %d seconds.\n\n",ans);
92 else
93 printf("Bugs cannot be fixed.\n\n");
94 }
95 return 0;
96 }
[题解]UVA658 It's not a Bug, it's a Feature!的更多相关文章
- 【最短路】【位运算】It's not a Bug, it's a Feature!
[Uva658] It's not a Bug, it's a Feature! 题目略 UVA658 Problem PDF上有 试题分析: 本题可以看到:有<=20个潜在的BUG,那 ...
- [有意思]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 ...
- poj1483 It's not a Bug, It's a Feature!
It's not a Bug, It's a Feature! Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 1231 ...
- 洛谷 题解 UVA658 【这不是bug,而是特性 It's not a Bug, it's a Feature!】
[题意] 补丁在修正\(BUG\)时,有时也会引入新的\(BUG\),假定有\(n(n<=20)\)个潜在\(BUG\),和\(m(m<=100)\)个补丁,每个补丁用两个长度为\(n\) ...
- 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.对于每个补丁 ...
随机推荐
- Golang 基准测试Benchmark
基准测试 Go语言标准库内置的 testing 测试框架提供了基准测试(benchmark)的能力,实现了对某个特定目标场景的某项性能指标进行定量的和可对比的测试. 基本规则 基准测试的代码文件必须以 ...
- 阿里巴巴如何进行测试提效 | 阿里巴巴DevOps实践指南
编者按:本文源自阿里云云效团队出品的<阿里巴巴DevOps实践指南>,扫描上方二维码或前往:https://developer.aliyun.com/topic/devops,下载完整版电 ...
- 微服务架构 | 2.2 Alibaba Nacos 的统一配置管理
目录 前言 1. Nacos 配置中心基础知识 1.1 Nacos 在配置中心中的功能 1.2 Nacos 配置管理 Data ID 的构成 1.3 Nacos 配置的回滚机制 1.4 Nacos 配 ...
- golang中的标准库time
时间类型 time.Time类型表示时间.我们可以通过time.Now()函数获取当前的时间对象,然后获取时间对象的年月日时分秒等信息.示例代码如下: func main() { current := ...
- golang中锁
一.什么场景下需要用到锁当程序中就一个线程的时候,是不需要加锁的,但是通常实际的代码不会只是单线程,有可能是多个线程同时访问公共资源,所以这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? ...
- MySQL函数学习(一)-----字符串函数
一.MySQL 字符串函数 \ 函 数 名 称 作 用 完 成 1 LENGTH 计算字符串字节长度 勾 2 CONCAT 合并字符串函数,返回结果为连接参数产生的字符串,参数可以是一个或多个 勾 3 ...
- 《HelloGitHub》第 70 期
兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...
- Protobuf 动态加载 .pb 文件并操作 Message
之前写了<Protobuf 动态加载 .proto 文件并操作 Message>.除了直接读取 .proto 文件之外,还有一种类似的方法.先把 .proto 文件编译成 .pb 文件,再 ...
- Vue 之 vue-cropper 组件实现头像裁剪功能
组件与api地址: npm地址地址:https://www.npmjs.com/package/vue-cropper/v/0.4.7 GitHub地址:https://github.com/xyxi ...
- ApacheCN Linux 译文集 20211129 更新
笨办法学 Linux 中文版 练习 0:起步 练习 1:文本编辑器,vim 练习 2:文本浏览器,少即是多 练习 3:Bash:Shell..profile..bashrc..bash_history ...