状态转移的最短路 隐式图搜索 UVA 658
紫书365
题目大意:给你n个全都是bug的东西,然后每次可以修复,给你修复前后的状态,问最后如果能把bug全都修复,最少需要多少时间。
思路:从最初状态开始,然后枚举bug即可。
表示priority里面的bool operator和单纯的sort的定义的大小于号是不一样的啊,如果你想用sort来计算struct从小到大的的话是这样的
struct Node{
int bugs, dist;
bool operator < (const Node &a) const{
return dist < a.dist;
}
Node(int b = , int d = ): bugs(b), dist(d){}
};
而优先队列是这样的
struct Node{
int bugs, dist;
bool operator < (const Node &a) const{
return dist > a.dist;
}
Node(int b = , int d = ): bugs(b), dist(d){}
};
区分一下大小于号就好了
//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int inf = 0x3f3f3f3f;
const int maxn = ( << ) + ;
const int maxm = + ;
int n, m;
int t[maxm], d[maxn];
bool vis[maxn];
char b[maxm][], e[maxm][];
struct Node{
int bugs, dist;
bool operator < (const Node &a) const{
return dist > a.dist;
}
Node(int b = , int d = ): bugs(b), dist(d){}
}; int solve(){
memset(vis, false, sizeof(vis));
memset(d, 0x3f, sizeof(d));
priority_queue<Node> que;
int tmp = ( << n) - ;
que.push(Node(tmp, ));
d[tmp] = ; vis[tmp] = ;
while (!que.empty()){
Node u = que.top(); que.pop();
if (u.bugs == ) return u.dist;
for (int i = ; i < m; i++){
int from = u.bugs;
bool flag = false;
for (int j = ; j < n; j++){
if (b[i][j] == '-' && (from & ( << j))) {flag = true; break;}
if (b[i][j] == '+' && !(from & ( << j))) {flag = true; break;}
}
if (flag) continue;
int to = from;
for (int j = ; j < n; j++){
if (e[i][j] == '-' && (from & ( << j))) to ^= << j;
if (e[i][j] == '+') to |= << j;
}
if (d[to] > d[from] + t[i] && !vis[to]){
d[to] = d[from] + t[i];
vis[to] = false;
que.push(Node(to, d[to]));
}
}
}
return -;
} int main(){
int kase = ;
while (scanf("%d%d", &n, &m) && n){
for (int i = ; i < m; i++){
scanf("%d", t + i);
scanf("%s%s", b[i], e[i]);
}
printf("Product %d\n", ++kase);
int ans = solve();
if (ans < ) printf("Bugs cannot be fixed.\n");
else printf("Fastest sequence takes %d seconds.\n", ans);
printf("\n");
}
return ;
}
状态转移的最短路 隐式图搜索 UVA 658的更多相关文章
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- 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!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】
题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...
- uva-321-暴力枚举-隐式图搜索
题意:给你n个房间,有许多灯的控制开关,i房间灯的开关在j房间,未开灯的房间不能进,i房间和j房间之间如果没有门,也不能从i进入到j,开始房间是1,并且灯是开着的,问你是否能够走到最后一个房间n,并且 ...
- uva10603-倒水问题-暴力枚举-隐式图搜索
题意: 给你三个杯子,a,b,c,没有刻度,刚开始c杯是满的,倒水的要求,要么倒出水的杯子倒空,要么倒入杯子倒满. 结果: 要求某个杯子内有d水量,并且倒出的水量最少,如果倒不出d水量,那么倒出d1( ...
- UVA - 10603 Fill(隐式图搜索)
题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...
随机推荐
- 运用bootstrap框架的时候 引入文件的问题
还要下个jquery,因为bootstrap的js是用jquery写的如果在同一个目录下<html><head><link href="css/bootstra ...
- Okhttp设置http缓存,在没有网络的情况下加载http缓存里面的内容
HTTP_CACHE_FILENAME为缓存地址根路径: private final String HTTP_CACHE_FILENAME = "HttpCache"; priva ...
- android 5.0 -- Palette
Palette用来从图片资源中获取颜色内容. 下面是个对颜色值使用的工具类: public class PaletteUtils { public static int getColorWithDef ...
- 浏览器 CSS 兼容写法的测试总结
做前端最讨厌的就是 IE6,7,8,虽然被淘汰的浏览器,但是在中国用户仍然很多,不可能像国外网站一样直接就不管它了,这样会流失很多流量啊. 现在有了IE9,IE10还好些,几乎和 Chrome,Fir ...
- URAL 1525 Path
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> us ...
- Tomcat 配置支持APR
对ARP支持,需要安装以下库: APR library JNI wrappers for APR used by Tomcat (libtcnative) OpenSSL libraries 其中JN ...
- 安卓请求服务器js文件下载到本地,版本号就下载
<?phpreturn array('CJ_V' => 'v0.15',) <script src="/js/reserve.js?<?=C('CJ_V')?> ...
- java增加时间
一个简单的东西. 示例如下: /** * 增加时间 * @param oldDate 老时间 * @param addtime 增加的时间 * @return */ public Date addDa ...
- table边框1px
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Openjudge-计算概论(A)-鸡尾酒疗法
描述: 鸡尾酒疗法,原指“高效抗逆转录病毒治疗”(HAART),由美籍华裔科学家何大一于1996年提出,是通过三种或三种以上的抗病毒药物联合使用来治疗艾 滋病.该疗法的应用可以减少单一用药产生的抗药性 ...