UVa 658 (Dijkstra) It's not a Bug, it's a Feature!
题意:
有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间。
求修复所有BUG的最短时间。
分析:
可以用n个二进制位表示这n个BUG的当前状态。最开始时所有BUG都存在,所以状态为n个1.目标状态是0
当打上一个补丁时,状态就会发生转移。因为有可能一个补丁要打多次,所以这个图不是DAG。
可以用Dijkstra算法,求起始状态到终态的最短路。代码中用了优先队列优化。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std; const int maxn = ;
const int maxm = + ;
const int INF = ; int n, m, t[maxm], dist[<<maxn], mark[<<maxn];
char before[maxm][maxn + ], after[maxm][maxn + ]; struct Node
{
int bugs, dist;
bool operator < (const Node& rhs) const
{//优先级大的排在前面,老是弄错=_=||
return dist > rhs.dist;
}
}; int solve()
{
for(int i = ; i < (<<n); ++i) { dist[i] = INF; mark[i] = ; }
priority_queue<Node> q; Node start;
start.dist = ;
start.bugs = (<<n) - ; dist[start.bugs] = ;
q.push(start); while(!q.empty())
{
Node u = q.top(); q.pop();
if(u.bugs == ) return u.dist;
if(mark[u.bugs]) continue;
mark[u.bugs] = ;
for(int i = ; i < m; ++i)
{
bool flag = true;
for(int j = ; j < n; ++j)
{//是否满足打补丁的条件
if(before[i][j] == '+' && !(u.bugs & (<<j))) { flag = false; break; }
if(before[i][j] == '-' && u.bugs & (<<j) ) { flag = false; break; }
}
if(!flag) continue; Node u2;
u2.dist = u.dist + t[i];
u2.bugs = u.bugs;
for(int j = ; j < n; ++j)
{//打完补丁以后的状态
if(after[i][j] == '+') u2.bugs |= ( << j);
if(after[i][j] == '-') u2.bugs &= ~( << j);
}
int &D = dist[u2.bugs];
if(u2.dist < D)
{
D = u2.dist;
q.push(u2);
}
}
} return -;
} int main()
{
//freopen("in.txt", "r", stdin); int kase = ;
while(scanf("%d%d", &n, &m) == && n && m)
{
for(int i = ; i < m; ++i) scanf("%d%s%s", &t[i], before[i], after[i]);
int ans = solve();
printf("Product %d\n", ++kase);
if(ans < ) puts("Bugs cannot be fixed.\n");
else printf("Fastest sequence takes %d seconds.\n\n", ans);
} return ;
}
代码君
UVa 658 (Dijkstra) It's not a Bug, it's a Feature!的更多相关文章
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- uva 10806 Dijkstra, Dijkstra. (最小费最大流)
uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了 ...
- [有意思]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 ...
- 【最短路】【位运算】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!(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! (最短路,经典)
题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了: ...
- UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...
随机推荐
- 2W/月和1W/月的工作,你会怎么选?
只看标题的话,肯定有不少人会选择月薪 2W 的工作,很明显,钱多嘛!但实际上,这里是有前提的,完整的问题如下: 一份月薪 2W,但加班无底线,基本没有自由时间的工作,和一份月薪 1W,但正常工作时长, ...
- call callb callp区别
对于刚刚接触ILE模式开发的初级菜鸟而言,想要搞清楚这三者的区别还是有点难度的.网上虽然一些帖子对这三者进行了比较,但是这些帖子或是语焉不详,或是高度概括.对于老鸟来说或许已经足矣,但是对于初级菜鸟而 ...
- ubuntu 安装dell无线网卡2
以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...
- VC++入门精通视频教程
1.1.Windows程序运行原理-1 上传日期:2012-03-19 09:18:50 相关摘要: - 在关闭一个windows窗口时,也关闭另一个吗 - 对纯面向对象的PHP程序有何看法? - ...
- 微软职位内部推荐-Senior Data Scientist
微软近期Open的职位: Extracting accurate, insightful and actionable information from data is part art and pa ...
- 选择排序O(n^2)与快速排序O(nlogn)的优越性代码体现
随机函数生成一个超大数组: [code]: #include <iostream> #include <stdio.h> #include<time.h> #inc ...
- Python系统调用——运行其他程序
转载:http://blog.csdn.net/ssihc0/article/details/7738527 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其 ...
- 使用Yeoman搭建 AngularJS 应用 (11) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/prepare-production.html 让我们发布这个应用 优化产品的文件 为了创建应用的产品版本,我们想做如下的事情 检查你的代码 ...
- Mybatis 示例之 SelectKey(转)
参考:http://blog.csdn.net/isea533/article/details/21153791 SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问 ...
- js验证码倒计时
var wait=59; function time(){ if(wait >= 0){ $("#buttons").val("" + wait + &q ...