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][ ...
随机推荐
- ECSHOP如何解决购物车中商品自动消失问题
最近有客户反映关于ECShop购物车的问题:需要加入多个商品到购物车时,发现之前加入到购物车的商品都自动消失了,只有最后一次加入购物车的商品在里面.那么,这是什么原因呢? 因为ECShop的SESSI ...
- Spark Streaming揭秘 Day14 State状态管理
Spark Streaming揭秘 Day14 State状态管理 今天让我们进入下SparkStreaming的一个非常好用的功能,也就State相关的操作.State是SparkStreaming ...
- unity3d 延迟处理方法
Invoke("方法名", 多少秒后执行); InvokeRepeating("方法名", 多少秒后执行,开始执行后隔多长时间再次执行一次); CancelIn ...
- SelectedValue,SelectedValuePath,SelectedValueBinding,DisplayMemberPath讲解
无论在Winform.WPF.ASP.NET中,数据绑定是我们经常使用的一个重要技术,我们经常会把相关类动态显示绑定到UI界面中,其中有几个比较重要的属性需要大家灵活运用. 那Combox来说明有两个 ...
- 十六、mysql 分区之 简单sql优化1
.使用 show session status like '%Com_%'; 可以查看当前连接的各个sql的执行频率 show global status like '%Com_%'; 可以查看从上次 ...
- Mvc生命周期深度剖析
客户端发送请求->IIS, UrlRouting模块对比URL, 默认如果该URL能对应到实体文件则退出MVC管道把控制权交还给IIS. 如果RegisterRoutes中的路由规则对比成功默认 ...
- 搭建Git Server
windows上如何搭建Git Server Git在版本控制方面,相比与SVN有更多的灵活性,对于开源的项目,我们可以托管到Github上面,非常方便,但是闭源的项目就会收取昂贵的费用.那么私有 ...
- C# - 高级方法参数
可选参数 -必须有个默认值,默认值必须是字面值,常量值,新对象实例或者默认值类型值. public List<string> GetWords( string sentence, bool ...
- python学习笔记15(面向对象编程)
虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界 ...
- Xcode8 重新配置 CocoaPods -替换阿里源
一.CocoaPods是什么? CocoaPods是一个负责管理iOS项目中第三方开源库的工具.CocoaPods的项目源码在Github上管理.该项目开始于2011年8月12日,在这两年多的时间里, ...