题目大意:

假定有n个潜在的bug和m个补丁,每个补丁用长为n的字符串表示。首先输入bug数目以及补丁数目。然后就是对m 个补丁的描述,共有m行。每行首先是一个整数,表明打该补丁所需要的时间。然后是两个字符串,地一个字符串 是对软件的描述,只有软件处于该状态下才能打该补丁该字符串的每一个位置代表bug状态(-代表该位置没bug,+代 表该位置有bug,0表示该位置无论有没有bug都可打补丁)。然后第二个字符串是对打上补丁后软件状态的描述 -代表该位置上的bug已经被修复,+表示该位置又引入了一个新的bug, 0表示该位置跟原来状态一样)。要求用最少 时间完成对软件的修复,即将所有位置全都置为0.

基本思路:

状态压缩一下转化为最短路来处理

代码如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue> using namespace std; typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1000000000+10;
int n,m,t[110],d[1<<20],vis[1<<20];
char before[110][25],after[110][25]; struct Node{
int bug,dist;
bool operator<(const Node& rhs)const{
return dist>rhs.dist;
}
}; int dijkstra(){
for(int i=0;i<(1<<n);i++){
d[i]=inf;
vis[i]=0;
}
priority_queue<Node>q;
Node start;
start.bug=(1<<n)-1;
start.dist=0;
q.push(start);
d[start.bug]=0;
while(!q.empty()){
Node x=q.top();q.pop();
if(x.bug==0) return x.dist;
if(vis[x.bug]) continue;
vis[x.bug]=1;
for(int i=0;i<m;i++){
bool pat=true;
for(int j=0;j<n;j++){
if(before[i][j]=='-'&&(x.bug&(1<<j))){
pat=false;
break;
}
if(before[i][j]=='+'&&!(x.bug&(1<<j))){
pat=false;
break;
}
}
if(!pat) continue;
Node next;
next.bug=x.bug;
next.dist=x.dist+t[i];
for(int j=0;j<n;j++){
if(after[i][j]=='-') next.bug&=~(1<<j);
if(after[i][j]=='+') next.bug|=(1<<j);
}
int &D=d[next.bug];
if(next.dist<D){
D=next.dist;
q.push(next);
}
}
}
return -1;
}
int main(){
int cas=0;
while(scanf("%d%d",&n,&m)==2&&n){
for(int i=0;i<m;i++){
scanf("%d%s%s",&t[i],&before[i],&after[i]);
}
int ans=dijkstra();
printf("Product %d\n",++cas);
if(ans<0){
printf("Bugs cannot be fixed.\n\n");
}else{
printf("Fastest sequence takes %d seconds.\n\n",ans);
}
}
return 0;
}

  

uva658 dijkstra+状态压缩的更多相关文章

  1. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  2. bzoj [POI2007]旅游景点atr 状态压缩+Dij

    [POI2007]旅游景点atr Time Limit: 30 Sec  Memory Limit: 357 MBSubmit: 2258  Solved: 595[Submit][Status][D ...

  3. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  4. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  5. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  6. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  7. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  8. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  9. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

随机推荐

  1. java面试题最容易犯错

    1. static 和 final 的用法 static 的作用从三个方面来谈,分别是静态变量.静态方法.静态类. 静态变量:声明为 static 的静态变量实质上就是全局变量,当声明一个对象时,并不 ...

  2. java 延时

    Java中主要有两种方法来实现延迟,即:Thread和Timer 1.普通延时用Thread.sleep(int)方法,这很简单.它将当前线程挂起指定的毫秒数.如try{Thread.currentT ...

  3. ERROR 2003 (HY000): Can't connect to MySQL server on '129.28.149.240' (111) mysql 无法远程连接

    环境: ubuntu  mysql 一. 查看端口 可以看到mysql监控的是本机Ip root@jiang:/etc/mysql/mysql.conf.d# netstat -apn|grep 33 ...

  4. 修改jquery默认的$

    一.使用JQuery.noConflict() 该方法的作用就是让Jquery放弃对$的所有权,将$的控制权交还给prototype.js,因为jquery.js是后引入的,所以最后拥有$控制权的是j ...

  5. 写一个自定义类加载器demo

    public class MyTest16 extends ClassLoader { private String classLoaderName; private String fileExten ...

  6. vue.js axios使用

    1. 自定义配置 /** * Created by superman on 17/2/16. * http配置 */ import axios from 'axios' import utils fr ...

  7. leetcode-165周赛-1275-找出井字棋的获胜者

    题目描述: 自己的提交: class Solution: def tictactoe(self, moves: List[List[int]]) -> str: p = [[0] * 3 for ...

  8. activemq消息中间件的依赖

    <dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artif ...

  9. python dir()详解

    Python dir() 函数 Python 内置函数 描述 dir() 函数不带参数时,返回当前范围内的变量.方法和定义的类型列表:带参数时,返回参数的属性.方法列表.如果参数包含方法__dir__ ...

  10. PHP curl_multi_select函数

    curl_multi_select — 等待所有cURL批处理中的活动连接 说明 int curl_multi_select ( resource $mh [, float $timeout = 1. ...