紫书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的更多相关文章

  1. 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)

    这道题用到了很多知识点, 是一道好题目.      第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算.     第二这里是隐式 ...

  2. [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索

    [HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...

  3. 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& ...

  4. 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)

    题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...

  5. uva 10274 Fans and Gems(隐式图搜索+模拟)

    Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...

  6. 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】

    题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...

  7. uva-321-暴力枚举-隐式图搜索

    题意:给你n个房间,有许多灯的控制开关,i房间灯的开关在j房间,未开灯的房间不能进,i房间和j房间之间如果没有门,也不能从i进入到j,开始房间是1,并且灯是开着的,问你是否能够走到最后一个房间n,并且 ...

  8. uva10603-倒水问题-暴力枚举-隐式图搜索

    题意: 给你三个杯子,a,b,c,没有刻度,刚开始c杯是满的,倒水的要求,要么倒出水的杯子倒空,要么倒入杯子倒满. 结果: 要求某个杯子内有d水量,并且倒出的水量最少,如果倒不出d水量,那么倒出d1( ...

  9. UVA - 10603 Fill(隐式图搜索)

    题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...

随机推荐

  1. 下载、安装jdk8(Windows下)并配置变量环境

    一.官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 点击下图中的downloa ...

  2. 在Eclipse中安装testNG插件

    1. 选择菜单:Help->Install New Software,点击Add按钮输入框中输入相应的Name:testNG和Location:http://beust.com/eclipse. ...

  3. 【界面优化】使用viewpagerindicator添加下划线滑动动画

    开源代码viewpagerindicator里面没有实现tab下划线切换过程中的移动动画,都是很突兀的多个fragement之间的切换,导致用户体验略差,google了下相关问题,发现一片博文: ht ...

  4. 淘淘商城_day04_课堂笔记

    今日大纲 实现首页的大广告位功能 实现内容管理系统 首页的大广告 什么是大广告 JS效果: 点击下面的序号选择查询哪个广告 自动切换 点击图片查询具体的页面 以上是由前端团队来开发. 数据结构 说明: ...

  5. AuthenticationManager, ProviderManager 和 AuthenticationProvider

    AuthenticationManager是一个接口: public interface AuthenticationManager { Authentication authenticate(Aut ...

  6. ggplot2 scale相关设置-坐标转换

    ggplot2 scale相关设置-坐标转换 在R中坐标轴转换有多种形式,包括对数转换,平方根转换以及坐标刻度前后进行调换 用到的函数分别有: scale_x_log10(...) scale_y_l ...

  7. 对一个表中所有列数据模糊查询adoquery

    如何用adoquery对一个表中所有列进行模糊查询: procedure TForm3.Button4Click(Sender: TObject); var ASql,AKey: string; I: ...

  8. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  9. js 中创建对象

    对象是什么 从JavaScript定义上讲对象是无序属性的集合,其属性可以包含基本值.对象或函数.也就是说对象是一组没有特定顺序的属性,每个属性会映射到一个值上,是一组键值对,值可以是数据或对象. 最 ...

  10. D - 娜娜梦游仙境系列——村民的怪癖

    D - 娜娜梦游仙境系列——村民的怪癖 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...