UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)
隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化,
二进制pre[i][0]表示可以出现的bug,那么u&pre[i][0] == u就表示u是可以出现的bug集合的子集,
pre[i][1]表示必须出现的bug,那么u|pre[i][i] != u表示把必须出现的bug添加到u中,u中bug增加表面bug不全在u中,这是不合法的。
正权最短路就dijkstra,用spfa以前某题狂T有阴影。被输出格式坑得不要不要的,如果是if(kas) putchar('\n');就会WA...
#include<bits/stdc++.h>
using namespace std; const int maxm = ;
const int maxn = ; int pre[maxm][],nxt[maxm][];
int cost[maxm];
int n,m; int dist[<<maxn]; typedef pair<int,int> Node;
#define fi first
#define se second //bitset<20> temp;
#define bug(u)\
temp = u; cout<<#u<<'='<<temp<<endl;
#define cer(x)\
cout<<"dist="<<x<<endl; const int INF = 0x3f3f3f3f; void dijkstra()
{
priority_queue<Node,vector<Node>,greater<Node> > q;
memset(dist,0x3f,sizeof(int)*(<<n));
q.push(Node(,(<<n)-));
dist[(<<n)-] = ;
while(q.size()){
Node x = q.top(); q.pop();
if(x.se == ) { printf("Fastest sequence takes %d seconds.\n",dist[]); return; }
if(x.fi != dist[x.se]) continue;
int u = x.se;
for(int i = ; i < m; i++){
if( (pre[i][]&u) == u && (pre[i][]|u) == u){
int v = (u&nxt[i][])|nxt[i][];
if(dist[v] > dist[u]+cost[i]){
dist[v] = dist[u] + cost[i];
q.push(Node(dist[v],v));
}
}
}
}
puts("Bugs cannot be fixed.");
} int main()
{
//freopen("in.txt","r",stdin);
int kas = ;
char s1[maxn+],s2[maxn+];
while(scanf("%d%d",&n,&m),n){
for(int i = ; i < m ; i++){
scanf("%d%s%s",cost+i,s1,s2);
nxt[i][] = nxt[i][] = pre[i][] = pre[i][] = ;
for(int j = ; j < n; j++){
if(s1[j] == '+') pre[i][] |= <<j;
if(s1[j] != '-') pre[i][] |= <<j;
if(s2[j] == '+') nxt[i][] |= <<j;
if(s2[j] != '-') nxt[i][] |= <<j;
}
}
printf("Product %d\n",++kas);
dijkstra();
putchar('\n');
}
return ;
}
UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)的更多相关文章
- 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!
这个题目巧妙之处在于用二进制的每个位1,0分别表示bug的有无,以及实施补丁对相应bug的要求以及实施后的对bug的影响. 软件bug的状态:1表示相应bug仍然存在,0表示已经修复.这样可以将软件的 ...
- UVa 658 It's not a Bug, it's a Feature! (状态压缩+Dijstra)
题意:首先给出n和m,表示有n个bug和m个补丁.一开始存在n个bug,用1表示一个bug存在0表示不存在,所以一开始就是n个1,我们的目的是要消除所有的bug, 所以目标状态就是n个0.对于每个补丁 ...
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- UVA 658 状态压缩+隐式图+优先队列dijstla
不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...
- It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)
Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS Memory Limit: 30000K Total Su ...
- 倒水问题UVA 10603——隐式图&&Dijkstra
题目 给你三个容量分别为 $a,b,c$ 的杯子,最初只有第3个杯子装满了水,其他两个杯子为空.最少需要到多少水才能让一个某个杯子中的水有 $d$ 升呢?如果无法做到恰好 $d$ 升,就让某个杯子里的 ...
- UVa 10603 Fill (BFS && 经典模拟倒水 && 隐式图)
题意 : 有装满水的6升的杯子.空的3升杯子和1升杯子,3个杯子中都没有刻度.不使用道具情况下,是否可量出4升水呢? 你的任务是解决一般性的问题:设3个杯子的容量分别为a, b, c,最初只有第3个杯 ...
随机推荐
- Flutter实战视频-移动电商-40.路由_Fluro的全局注入和使用方法
40.路由_Fluro的全局注入和使用方法 路由注册到顶层,使每个页面都可以使用,注册到顶层就需要在main.dart中 main.dart注册路由 注入 onGenerateRoute是Materi ...
- PLSQL导入导出oracle表 表空间
PLSQL导入导出表的正确步骤 原来总是直接 tools->import talbes->Oracle Import结果发现有的时候会出错:有的表不能正确导入, baidu+googel解 ...
- flex(1)
flex使用的actionscript语言遵守ECMA-262标准,这与javascript语言是一致的,由此可见二者语法的相似.
- 洛谷 - P1663 - 山 - 半平面交
https://www.luogu.org/problemnew/show/P1663 给定山的性状,求一个最低点可以看见所有的地方. 就是半平面交. 粘贴全家福: #include<bits/ ...
- 无法打开包括文件:“SDKDDKVer.h”: No such file or directory
在已经装有Visual Studio 2010的系统中,同时安装Visual Studio 2012,安装过程很顺利,但到使用VS2013时,却出问题了. 本文主要介绍:VS中新建工程编译时出现,“无 ...
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...
- lightoj1259 【素数预处理】
题意: 输出有多少对满足条件的(a,b) both a and b are prime; a+b=n a<=b; 思路: 一开始想的就是打表一个素数数组,然后还去二分..mdzz..直接判断一下 ...
- [Xcode 实际操作]八、网络与多线程-(23)多线程的同步与异步的区别
目录:[Swift]Xcode实际操作 本文将演示线程的同步与异步的区别. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 异步线程的运行,是没有按照顺序执行的. ...
- springboot与shiro和mybatis和mysql
测试项目已上传到GitHub:https://github.com/xiaostudy/springboot_shiro_test1 1.创建springboot项目 <!-- 数据库连接池 - ...
- __str__,__repr__
目录 __str__ __repr__ __str__ 打印时触发 class Foo: pass obj = Foo() print(obj) <__main__.Foo object at ...