题意:首先给出n和m,表示有n个bug和m个补丁。一开始存在n个bug,用1表示一个bug存在0表示不存在,所以一开始就是n个1,我们的目的是要消除所有的bug,

所以目标状态就是n个0。对于每个补丁,会给出使用这个补丁的时间,另外会给出两个长度为n的字符串,第一个字符串表示这个补丁适用于什么情况下的bug,

第二个字符串表示使用完这个补丁后原来的bug会变成怎么样。先说第一个字符串,s[i]=’0’,表示第i个bug存在与否都无所谓;s[i]=’+’,

表示第i个bug一定要存在;s[i]=’-‘,表示第i个bug必须不存在;能不能使用这个补丁,就要看当前bug的状态是不是能不能全部满足第一个字符串,能的话就可以使用。

第二个字符串表示使用完后的情况,ss[i]=’0’,表示第i个bug保持不变,原来是1就1是0就0;ss[i]=’+’,表示第i个bug必须为1;ss[i]=’-‘,表示第i个bug必须为0。

最终题目要求解的就是消除所有的bug并且用时最短,输出最短时间,如果bug不可能被完全消除那么就输出。

析:我们首先对所有的状态进行压缩,然后对每个状态进行搜索,使用最短路,最后并对状态进行判断能不能进行打补丁,最后看

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 1e6 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Patch{
int bm, bp;
int am, ap;
int t;
};
Patch a[110];
char s1[25], s2[25];
int d[1<<20]; void print(int j){
for(int i = 0; i < n; ++i)
if(j & (1<<i)) putchar('1');
else putchar('0');
} int dijstra(int s){
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, s));
fill(d, d+(1<<20), INF);
d[s] = 0; while(!pq.empty()){
P p = pq.top(); pq.pop();
if(p.second == 0) return p.first;
int v = p.second;
if(d[v] < p.first) continue;
for(int i = 0; i < m; ++i){
if(((p.second & a[i].bp) == a[i].bp) && ((p.second & a[i].bm) == 0)){
int u = (p.second | a[i].ap) & a[i].am;
if(d[u] > d[v] + a[i].t){
d[u] = d[v] + a[i].t;
pq.push(P(d[u], u));
}
}
}
}
return -1;
} int main(){
int kase = 0;
while(scanf("%d %d", &n, &m) && m+n){
memset(a, 0, sizeof a);
for(int i = 0; i < m; ++i){
scanf("%d", &a[i].t);
scanf("%s", s1);
scanf("%s", s2);
a[i].am = (1<<n)-1;
for(int j = 0; j < n; ++j){
if(s1[j] == '+') a[i].bp |= (1<<j);
else if(s1[j] == '-') a[i].bm |= (1<<j);
if(s2[j] == '+') a[i].ap |= (1<<j);
else if(s2[j] == '-') a[i].am ^= (1<<j);
}
}
int ans = dijstra((1<<n)-1);
printf("Product %d\n", ++kase);
if(ans == -1) printf("Bugs cannot be fixed.");
else printf("Fastest sequence takes %d seconds.", ans);
puts("\n");
}
return 0;
}

是不是能完全打完所有的补丁,

由于是要时间是最少,所以我们可以用优先队列进行维护。

代码如下:

UVa 658 It's not a Bug, it's a Feature! (状态压缩+Dijstra)的更多相关文章

  1. UVA 658 It's not a Bug, it's a Feature! (最短路,经典)

    题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了: ...

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

  3. UVA 658 It's not a Bug, it's a Feature!

    这个题目巧妙之处在于用二进制的每个位1,0分别表示bug的有无,以及实施补丁对相应bug的要求以及实施后的对bug的影响. 软件bug的状态:1表示相应bug仍然存在,0表示已经修复.这样可以将软件的 ...

  4. UVA - 658 It's not a Bug, it's a Feature! (隐式图的最短路,位运算)

    隐式的图搜索,存不下边,所以只有枚举转移就行了,因为bug的存在状态可以用二进制表示,转移的时候判断合法可以用位运算优化, 二进制pre[i][0]表示可以出现的bug,那么u&pre[i][ ...

  5. UVa 658 (Dijkstra) It's not a Bug, it's a Feature!

    题意: 有n个BUG和m个补丁,每个补丁用一个串表示打补丁前的状态要满足的要求,第二个串表示打完后对补丁的影响,还有打补丁所需要的时间. 求修复所有BUG的最短时间. 分析: 可以用n个二进制位表示这 ...

  6. UVA 658 状态压缩+隐式图+优先队列dijstla

    不可多得的好题目啊,我看了别人题解才做出来的,这种题目一看就会做的实在是大神啊,而且我看别人博客都看了好久才明白...还是对状态压缩不是很熟练,理解几个位运算用了好久时间.有些题目自己看着别人的题解做 ...

  7. [有意思]The IT workers of Star Wars -- That's not a bug. It's a feature

    Yeah, that Artoo is kinda mouthy... ... now select, "restore to factory settings." That'll ...

  8. uva 11195 Another queen (用状态压缩解决N后问题)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. poj1483 It's not a Bug, It's a Feature!

    It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1231   ...

随机推荐

  1. pandas-事例练习

    补充: DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False) 功能:根据各标签的值中是否存在缺失数据 ...

  2. bitmap进行个数统计

    昨天看了编程珠玑第一章的内容, 发现bitmap对于统计某一个范围内的整数个数效率很高, 就自己实现了一下: 这是原始的bitmap, 用于统计0~maxSize是否出现, 也可以用于排序 publi ...

  3. MAC如何查看某个端口的占用情况

    执行如下命令: lsof -i tcp:8080 #8080为查询的端口号 会展示该端口的使用情况,然后kill -9 PID的值即可关闭该端口

  4. windows常用快捷键(转载)

    f1 显示当前程序或者windows的帮助内容. f2 当你选中一个文件的话,这意味着“重命名” f3 当你在桌面上的时候是打开“查找:所有文件” 对话框 f10或alt 激活当前程序的菜单栏 win ...

  5. makefile编写---.c .cpp 混合编译makefile 模板

    # c.cpp混合编译的makefile模板 # # BIN = client_system BASE_INSTALL_DIR := /opt/arm-2009q1 BUILD_TOOL_DIR := ...

  6. quick-cocos2d-x教程3:程序框架内文件夹分析之docs文件夹

    如今我们分析框架中的docs文件夹.看看这个文档文件夹中,究竟放了那些对我们实用的东西. docs文件夹分析 UPGRADE_TO_2_2_3.md 就是讲升级的变化.详细说明:quick-cocos ...

  7. iOS开发-14款状态栏(StatusBar)开源软件

    本文转载至 http://mobile.51cto.com/hot-418125.htm 之前逛街看到移动做推广,有一个定位应用挺好的,合理的利用了状态栏,做了一些消息提醒和隐藏动画,自己回家就做了一 ...

  8. Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..

    https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...

  9. cc、gcc、g++区别

    gcc是C编译器:g++是C++编译器:linux下cc一般是一个符号连接,指向gcc:gcc和g++都是GUN(组织)的编译器.而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Lin ...

  10. shapes

    接口 shape package shape; public abstract interface shape { public abstract void Draw(); public abstra ...