(beginer) 最短路 UVA 658 It\'s not a Bug, it\'s a Feature!

  It's not a Bug, it's a Feature! 

It is a curious fact that consumers buying a new software product generally do not expect the software to be bug-free. Can you imagine buying a car whose steering wheel only turns to the right? Or a CD-player that plays only CDs with country music on them? Probably not. But for software systems it seems to be acceptable if they do not perform as they should do. In fact, many software companies have adopted the habit of sending out patches to fix bugs every few weeks after a new product is released (and even charging money for the patches).

Tinyware Inc. is one of those companies. After releasing a new word processing software this summer, they have been producing patches ever since. Only this weekend they have realized a big problem with the patches they released. While all patches fix some bugs, they often rely on other bugs to be present to be installed. This happens because to fix one bug, the patches exploit the special behavior of the program due to another bug.

More formally, the situation looks like this. Tinyware has found a total of n bugs $B= \{b_1, b_2, \dots, b_n\}$ in their software. And they have released m patches $p_1, p_2, \dots, p_m$. To apply patch pi to the software, the bugs $B^+_i \subseteq B$ have to be present in the software, and the bugs $B^-_i \subseteq B$ must be absent (of course $B^+_i \cap B^-_i = \emptyset$ holds). The patch then fixes the bugs $F^-_i \subseteq B$ (if they have been present) and introduces the new bugs $F^+_i \subseteq B$ (where, again, $F^-_i \cap B^-_i = \emptyset$).

Tinyware's problem is a simple one. Given the original version of their software, which contains all the bugs in B, it is possible to apply a sequence of patches to the software which results in a bug- free version of the software? And if so, assuming that every patch takes a certain time to apply, how long does the fastest sequence take?

Input 

The input contains several product descriptions. Each description starts with a line containing two integers n and m, the number of bugs and patches, respectively. These values satisfy $1 \le n \le 20$ and $1 \le m \le 100$. This is followed by m lines describing the m patches in order. Each line contains an integer, the time in seconds it takes to apply the patch, and two strings of n characters each.

The first of these strings describes the bugs that have to be present or absent before the patch can be applied. The i-th position of that string is a ``+'' if bug bi has to be present, a ``-'' if bug bi has to be absent, and a `` 0'' if it doesn't matter whether the bug is present or not.

The second string describes which bugs are fixed and introduced by the patch. The i-th position of that string is a ``+'' if bug bi is introduced by the patch, a ``-'' if bug bi is removed by the patch (if it was present), and a ``0'' if bug bi is not affected by the patch (if it was present before, it still is, if it wasn't, is still isn't).

The input is terminated by a description starting with n = m = 0. This test case should not be processed.

Output 

For each product description first output the number of the product. Then output whether there is a sequence of patches that removes all bugs from a product that has all n bugs. Note that in such a sequence a patch may be used multiple times. If there is such a sequence, output the time taken by the fastest sequence in the format shown in the sample output. If there is no such sequence, output `` Bugs cannot be fixed.''.

Print a blank line after each test case.

Sample Input 

3 3
1 000 00-
1 00- 0-+
2 0-- -++
4 1
7 0-0+ ----
0 0

Sample Output 

Product 1
Fastest sequence takes 8 seconds.

Product 2
Bugs cannot be fixed.


题意:一开始有n个bug,然后给出一些补丁的启动需要的条件和启动后bug会变成什么情况,然后问最少需要多少秒才能把所有bug修复好。

思路:因为只有20个bug,所以我们用二进制表示,1表示bug显示,0表示bug不显示。然后我们用位运算判断一下符不符合补丁的要求,然后再用位运算算出转移后的状态,然后套dijkstra的模板。

代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn = 1<<21;
int maxstate;
int d[maxn];
bool done[maxn];
char buffer[22];
int n , m;

struct state
{
	state(int uu,int dd) : u(uu) , dist(dd) { }
	int u;
	int dist;
};

inline bool operator < (const state & s1,const state & s2)
{
	return s1.dist > s2.dist;
}

struct Patch
{
	int u_abt;
	int u_prt;
	int v_abt;
	int v_prt;
	int w;
}patch[110];

int GetPresent()
{
	int ret = 0;
	for (int i = 0 ; i < n ; ++i) 
		ret = (ret*2+(buffer[i]=='+'));
	return ret;
}

int GetAbsent()
{
	int ret = 0;
	for (int i = 0 ; i < n ; ++i)
		ret = (ret*2+(buffer[i]=='-'));
	return ret;
}

void input()
{
	maxstate = (1<<n)-1;
	for (int i = 0 ; i < m ; ++i)
	{
		scanf("%d",&patch[i].w);
		scanf("%s",buffer);
		patch[i].u_prt = GetPresent();
		patch[i].u_abt = GetAbsent();
		scanf("%s",buffer);
		patch[i].v_prt = GetPresent();
		patch[i].v_abt = GetAbsent();
	}
}

bool match(int s , int i)
{
	if ((patch[i].u_prt&s)!=patch[i].u_prt) return false;
	if ((~s&maxstate&patch[i].u_abt)!=patch[i].u_abt) return false;
	return true;
}

int tostate(int s,int i)
{
	return patch[i].v_prt | s & ~patch[i].v_abt;
}

void spfa()
{
	memset(d,0x3f,sizeof(d));
	memset(done,0,sizeof(done));
	priority_queue<state> q;
	int u = maxstate;
	d[u] = 0;
	q.push(state(u,0));
	int v , w;
	while (q.size())
	{
		state tmp = q.top(); q.pop();
		u = tmp.u;
		if (done[u]) continue;
		done[u] = true;
		if (u==0) { printf("Fastest sequence takes %d seconds.\n",tmp.dist); return; }
		for (int i = 0 ; i < m ; ++i) if (match(u,i))
		{
			w = patch[i].w;
			v = tostate(u,i);
			if (d[v] > d[u]+w) {
				d[v] = d[u]+w;
				q.push(state(v,d[v]));
			}
		}
	}
	printf("Bugs cannot be fixed.\n");
}

int main()
{
	int k = 0;
	while (scanf("%d%d",&n,&m),n+m)
	{
		++k;
		input();
		printf("Product %d\n",k);
		spfa();
		cout << endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值