UVA - 658 最短路
思路:通过前后两种状态建立一条边,利用Dijsktra就可以做了。
注意利用二进制优化。
AC代码
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = (1 << 20) + 5, maxm = 100 + 5;
int n, m, d[maxn], vis[maxn];
char before[maxm][25], after[maxm][25];
int cost[maxm];
struct Node{
int bug, dist;
Node(){}
Node(int bug, int dis):bug(bug),dist(dis){}
bool operator < (const Node& p) const {
return dist > p.dist;
}
};
int Dijsk(int u) {
memset(d, inf, sizeof(d));
memset(vis, 0, sizeof(vis));
priority_queue<Node>Q;
Q.push(Node(u, 0));
d[u] = 0;
while(!Q.empty()) {
Node p = Q.top(); Q.pop();
int u = p.bug;
if(u == 0) return d[u];
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < m; ++i) {
bool ok = true;
for(int j = 0; j < n; ++j) {
if(before[i][j] == '+' && !(u & (1 << j))) {ok = false; break;}
if(before[i][j] == '-' && (u & (1 << j))) {ok = false; break;}
}
if(!ok) continue; //不能打补丁
Node v = Node(u, p.dist + cost[i]);
for(int j = 0; j < n; ++j) {
if(after[i][j] == '-') v.bug &= ~(1 << j);
if(after[i][j] == '+') v.bug |= (1 << j);
}
if(v.dist < d[v.bug] || d[v.bug] < 0) {
d[v.bug] = v.dist;
Q.push(v);
}
}
}
return -1;
}
int main() {
int kase = 0;
while(scanf("%d%d", &n, &m) == 2 && n && m) {
for(int i = 0; i < m; ++i) {
scanf("%d%s%s", &cost[i], before[i], after[i]);
}
int ans = Dijsk((1 << n) - 1);
printf("Product %d\n", ++kase);
if(ans == -1) printf("Bugs cannot be fixed.\n");
else printf("Fastest sequence takes %d seconds.\n", ans);
printf("\n");
}
return 0;
}
如有不当之处欢迎指出!
UVA - 658 最短路的更多相关文章
- UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...
- UVA 658 It's not a Bug, it's a Feature! (最短路,经典)
题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了: ...
- 状态转移的最短路 隐式图搜索 UVA 658
紫书365 题目大意:给你n个全都是bug的东西,然后每次可以修复,给你修复前后的状态,问最后如果能把bug全都修复,最少需要多少时间. 思路:从最初状态开始,然后枚举bug即可. 表示priorit ...
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
- uva 10269 最短路
求两次最短路 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- 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表示已经修复.这样可以将软件的 ...
随机推荐
- 移动端 iphone锁屏文字效果
简易的仿照iphone 效果 笔记备份 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Conten ...
- java项目导出war包
@参考文档 假如项目名称为yanan,进入到yanan目录下打开cmd 执行:jar -cvf yanan.war ./* 命令详解: @参考文档1 @参考文档2 jar:jar工具是个java应用程 ...
- java8大基本数据类型
基本类型 字节数 位数 最大值 最小值 byte 1byte 8bit 2^7 - 1 -2^7 short 2byte 16bit 2^15 - 1 -2^15 int 4byte 32bit 2^ ...
- Bootstrap fileinput:文件上传插件的基础用法
官网地址:http://plugins.krajee.com/ 官网提供的样例:http://plugins.krajee.com/file-input/demo 基础用法一 导入核心CSS及JS文件 ...
- C# String StringBuilder 区别
这篇博客,纯参考.主要为了自己也复习一遍,过一下其中的原理. string aTest = "abc";//分配固定的内存大小 aTest += "ddd"; ...
- 【Shell脚本学习指南笔记】重定向文件描述符 2>&1
如: make > results 2>&1 重定向 > results让文件描述符1(标准输出)作为文件results,接下来的重定向2>&1有两个部分.2& ...
- PHP操作Memcached的方法汇总
memcached非关系型数据库安装.php中的memcache的扩展安装.以及php中的memcached的扩展安装可以参考: http://www.cnblogs.com/phpstudy2015 ...
- 【mac】mac os X更新High Sierra后出现的问题
今天更新了一下macbook pro的系统到10.13.1版本,出现了几个小问题,总结一下解决方案: git客户端无法使用 解决方案如下: http://blog.csdn.net/kedongjun ...
- SQL Server判断小数位数
项目中需要写一规则,目的是判断数值的小数位数,可以分为2中情况. 1.直接以小数点为分界点,小数点后的数据表示小数的位数,此种情况比较简单,直接使用CHARINDEX函数就可以搞定 其中CHARIND ...
- 解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE
问题 如下: 2017-07-16 08:50:57.436 INFO 13524 --- [ main] c.p.p.web.PointshopWebApplication ...