题意:

有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间。

求修复所有BUG的最短时间。

分析:

可以用n个二进制位表示这n个BUG的当前状态。最开始时所有BUG都存在,所以状态为n个1.目标状态是0

当打上一个补丁时,状态就会发生转移。因为有可能一个补丁要打多次,所以这个图不是DAG。

可以用Dijkstra算法,求起始状态到终态的最短路。代码中用了优先队列优化。

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int maxn = ;
  7. const int maxm = + ;
  8. const int INF = ;
  9.  
  10. int n, m, t[maxm], dist[<<maxn], mark[<<maxn];
  11. char before[maxm][maxn + ], after[maxm][maxn + ];
  12.  
  13. struct Node
  14. {
  15. int bugs, dist;
  16. bool operator < (const Node& rhs) const
  17. {//优先级大的排在前面,老是弄错=_=||
  18. return dist > rhs.dist;
  19. }
  20. };
  21.  
  22. int solve()
  23. {
  24. for(int i = ; i < (<<n); ++i) { dist[i] = INF; mark[i] = ; }
  25. priority_queue<Node> q;
  26.  
  27. Node start;
  28. start.dist = ;
  29. start.bugs = (<<n) - ;
  30.  
  31. dist[start.bugs] = ;
  32. q.push(start);
  33.  
  34. while(!q.empty())
  35. {
  36. Node u = q.top(); q.pop();
  37. if(u.bugs == ) return u.dist;
  38. if(mark[u.bugs]) continue;
  39. mark[u.bugs] = ;
  40. for(int i = ; i < m; ++i)
  41. {
  42. bool flag = true;
  43. for(int j = ; j < n; ++j)
  44. {//是否满足打补丁的条件
  45. if(before[i][j] == '+' && !(u.bugs & (<<j))) { flag = false; break; }
  46. if(before[i][j] == '-' && u.bugs & (<<j) ) { flag = false; break; }
  47. }
  48. if(!flag) continue;
  49.  
  50. Node u2;
  51. u2.dist = u.dist + t[i];
  52. u2.bugs = u.bugs;
  53. for(int j = ; j < n; ++j)
  54. {//打完补丁以后的状态
  55. if(after[i][j] == '+') u2.bugs |= ( << j);
  56. if(after[i][j] == '-') u2.bugs &= ~( << j);
  57. }
  58. int &D = dist[u2.bugs];
  59. if(u2.dist < D)
  60. {
  61. D = u2.dist;
  62. q.push(u2);
  63. }
  64. }
  65. }
  66.  
  67. return -;
  68. }
  69.  
  70. int main()
  71. {
  72. //freopen("in.txt", "r", stdin);
  73.  
  74. int kase = ;
  75. while(scanf("%d%d", &n, &m) == && n && m)
  76. {
  77. for(int i = ; i < m; ++i) scanf("%d%s%s", &t[i], before[i], after[i]);
  78. int ans = solve();
  79. printf("Product %d\n", ++kase);
  80. if(ans < ) puts("Bugs cannot be fixed.\n");
  81. else printf("Fastest sequence takes %d seconds.\n\n", ans);
  82. }
  83.  
  84. return ;
  85. }

代码君

UVa 658 (Dijkstra) It's not a Bug, it's a Feature!的更多相关文章

  1. 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)

    题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...

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

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

  3. uva 10806 Dijkstra, Dijkstra. (最小费最大流)

    uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了 ...

  4. [有意思]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 ...

  5. 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   ...

  6. 【最短路】【位运算】It's not a Bug, it's a Feature!

    [Uva658] It's not a Bug, it's a Feature! 题目略 UVA658 Problem PDF上有 试题分析:     本题可以看到:有<=20个潜在的BUG,那 ...

  7. 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& ...

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

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

  9. UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)

    隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...

随机推荐

  1. jQuery基础与实例

    一.简介 1.什么是jQuery jQuery是一个轻量级.快速简洁的javaScript库,能让我们方便快捷的选择元素操作元素属性. 2.下载地址 3.jQuery使用方式 $("div& ...

  2. 十四、mysql 分区之 HASH && KEY

    .hash分区 PS::个人觉得HASH分区很好很强大,简单确分布极其均匀 创建实例: CREATE TABLE HASH_EMP ( tid int, tname ) ) PARTITION ; 将 ...

  3. iOS 深复制&浅复制

        1.无论是深复制还是浅复制,被复制的对象类型是不变的.此对象类型具有什么功能就具有什么功能,不会因为自行修改了返回对象的指针类型而改变.   比如: 这里的str和str1的值和指针地址完全一 ...

  4. 【BZOJ 1202】 [HNOI2005]狡猾的商人

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  5. 【BZOJ 1015】[JSOI2008]星球大战starwar

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  6. jquery pass parameter to ajax callback

    $('.del').on('click', function () { var id = $(this).attr('id'); var url = '/m/g2_content_del/' + id ...

  7. 1588: [HNOI2002]营业额统计 - BZOJ

    Description营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  8. hdu 3778

    简单的dfs  但繁琐的可以了 0.0 #include<cstdio> #include<cstring> #include<algorithm> using s ...

  9. 转--利用函数模板技术,写一个简单高效的 JSON 查询器

    http://www.cnblogs.com/index-html/archive/2012/07/18/js_select.html http://www.ibm.com/developerwork ...

  10. Qt读写二进制文件

    http://blog.csdn.net/mjlsuccess/article/details/22194653 http://www.cnblogs.com/weiweiqiao99/archive ...