UVA 658 - It's not a Bug, it's a Feature!(最短路)
来源:互联网 发布:沙丁鱼流量软件下载 编辑:程序博客网 时间:2024/04/28 21:04
It's not a Bug, it's a Feature!
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 in their software. And they have released m patches . To apply patch pi 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 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 integersn and m, the number of bugs and patches, respectively. These values satisfy and . 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 31 000 00-1 00- 0-+2 0-- -++4 17 0-0+ ----0 0
Sample Output
Product 1Fastest sequence takes 8 seconds.Product 2Bugs cannot be fixed.
题意:给定n长度的m个bug修复方式。求最少时间修复。
思路:最短路,SPFA,建图利用位运算。参考http://blog.csdn.net/mr_zys/article/details/8917261
代码:
#include <stdio.h>#include <string.h>#include <queue>using namespace std;#define INF 0x3f3f3f3fconst int N = 105;const int MAXN = 2000005;int n, m, time[N], start[2][N], end[2][N], d[MAXN], vis[MAXN], cas= 0;char s1[N], s2[N];queue<int>q;void SPFA() {int star = (1<<n) - 1;memset(d, INF, sizeof(d));memset(vis, 0, sizeof(vis));d[star] = 0;q.push(star);while (!q.empty()) {int u = q.front(); q.pop();vis[u] = 0;for (int i = 0; i < m; i ++) {int v;int v1 = u|start[1][i];int v2 = u&start[0][i];if (v1 == u && v2 == u) {v = u|end[1][i];v = v&end[0][i];if (d[v] > d[u] + time[i]) {d[v] = d[u] + time[i];if (!vis[v]) {vis[v] = 1;q.push(v);}}}}}}void init() {memset(start, 0, sizeof(start));memset(end, 0, sizeof(end));for (int i = 0; i < m; i ++) {scanf("%d%s%s", &time[i], s1, s2);for (int j = 0; j < n; j ++) {if (s1[j] == '+') start[1][i] += 1<<(n - j - 1);if (s1[j] != '-') start[0][i] += 1<<(n - j - 1);if (s2[j] == '+') end[1][i] += 1<<(n - j - 1);if (s2[j] != '-') end[0][i] += 1<<(n - j - 1);}}}void solve() {init();SPFA();printf("Product %d\n", ++cas);if (d[0] != INF)printf("Fastest sequence takes %d seconds.\n", d[0]);else printf("Bugs cannot be fixed.\n");printf("\n");}int main() {while (~scanf("%d%d", &n, &m) && n || m) {solve();}return 0;}
- Uva 658 - It's not a Bug, it's a Feature!//最短路
- UVA 658 - It's not a Bug, it's a Feature!(最短路)
- (beginer) 最短路 UVA 658 It\'s not a Bug, it\'s a Feature!
- UVa 658It's not a Bug, it's a Feature! -- 最短路dijkstra
- uva 658 It's not a Bug, it's a Feature! 最短路
- UVA 658 It's not a Bug, it's a Feature! (特殊图最短路)
- UVa 658 It's not a Bug, it's a Feature!(最短路问题)
- uva - 658 It's not a Bug, it's a Feature! 最短路
- UVA 658 It's not a Bug, it's a Feature!(状压+隐式图+最短路)
- uva 658 It's not a Bug, it's a Feature! 最短路dijstra
- It's not a Bug, it's a Feature! UVA
- It's not a Bug, it's a Feature! UVA
- It's not a Bug, it's a Feature! UVA
- It's not a Bug, it's a Feature! UVA
- HDU 1818 It's not a Bug, It's a Feature! 最短路
- UVA658-It's not a Bug, it's a Feature!-最短路
- UVA658[It's not a Bug, it's a Feature!] BellmanFord || Dijkstra 求最短路
- 658 - It's not a Bug, it's a Feature!
- rtems系统在mini2440上的移植
- 编程开发观点杂谈(3)
- adf oracle input Validator
- 【AC自动机】
- OpenCV操作PNG格式图像
- UVA 658 - It's not a Bug, it's a Feature!(最短路)
- delphi中DateTimePicker同时修改日期和时间的实现
- PL/SQL语言环境设置
- c语言程序的存储区域与const关键字的使用方法
- 求帮助 http://bbs.tianya.cn/post-no04-2361448-1.shtml
- POJ2362:Square(DFS)
- unity3d editor splitview
- JAVA的零碎知识1
- 初代 iPhone:那道不清说不尽的故事