It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)


Language:
Default

It‘s not a Bug, It‘s a Feature!

Time Limit: 5000MS   Memory Limit: 30000K
Total Submissions: 1353   Accepted: 516

Description

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 = {b1, b2, ..., bn} in their software. And they have released m patches p1, p2, ..., pm. To apply patch pi to the software, the bugs Bi+ in B have to be present
in the software, and the bugs Bi- in B must be absent (of course Bi+ ∩ Bi- = Φ). The patch then fixes the bugs Fi- in B (if they have been present) and introduces the new bugs Fi+ in B (where, again, Fi+
Fi- = Φ).

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 <= n <= 20 and 1 <= m <= 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.

Source

Southwestern European Regional Contest 1998

这个题拿到手并不会做,没有好的思路,然后就看了网上的题解。第一次碰到不建图也能SPFA的,又学习了。另外这一题的位运算处理也非常巧妙,这是我不熟悉的,先放在这里,以后多来看几遍。

參考这两篇博客,写的非常好:http://www.cnblogs.com/scau20110726/archive/2012/12/16/2820739.html

http://www.cnblogs.com/staginner/archive/2011/10/25/2223489.html

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 25;
const int MAXN = 111;
const int MAXM = (1<<20)+100;
const int N = 1005;

char s1[maxn],s2[maxn];
int s[2][MAXN],t[2][MAXN],cost[MAXN];
int dist[MAXM];
bool inq[MAXM];
int n,m;

void SPFA()
{
    int i,j;
    mem(inq,false);
    mem(dist,INF);
    queue<int>Q;
    int start=(1<<n)-1;
    Q.push(start);
    inq[start]=true;
    dist[start]=0;
    while (!Q.empty())
    {
        int u=Q.front(); Q.pop();
        inq[u]=false;
        for (i=0;i<m;i++)
        {
            if ((u|s[1][i])==u&&(u&s[0][i])==u)
            {
                int v=u;
                v|=t[1][i];
                v&=t[0][i];
                if (dist[v]>dist[u]+cost[i])
                {
                    dist[v]=dist[u]+cost[i];
                    if (!inq[v])
                    {
                        inq[v]=true;
                        Q.push(v);
                    }
                }
            }
        }
    }
//    for(i=0;i<=start;i++)
//        pf("%d ",dist[i]);
//    pf("\n");
    if (dist[0]==INF)
        pf("Bugs cannot be fixed.\n");
    else
        pf("Fastest sequence takes %d seconds.\n",dist[0]);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j,cas=0;
    while (sff(n,m))
    {
        if (n==0&&m==0) break;
        mem(s,0);
        mem(t,0);
        for (i=0;i<m;i++)
        {
            scanf("%d%s%s",&cost[i],s1,s2);
            for (j=0;j<n;j++)
            {
                if (s1[j]==‘+‘)
                    s[1][i]+=(1<<j);
                if (s1[j]!=‘-‘)
                    s[0][i]+=(1<<j);
                if (s2[j]==‘+‘)
                    t[1][i]+=(1<<j);
                if (s2[j]!=‘-‘)
                    t[0][i]+=(1<<j);
            }
        }
        pf("Product %d\n",++cas);
        SPFA();
        pf("\n");
    }
    return 0;
}

It's not a Bug, It's a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

时间: 2024-12-25 05:31:00

It&#39;s not a Bug, It&#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)的相关文章

【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 << pos)); 将s中二进制第k位变成1的处理方式: s = s | (1 << pos); 2.二进制运算: [1] & : 1 & 1 = 1 , 1 & 0 = 0 , 0 & 0 = 0; 快速判断奇偶性: if(a & 1);//为奇数

hdu1818 It&#39;s not a Bug, It&#39;s a Feature!(隐式图最短路径Dijkstra)

题目链接:点击打开链接 题目描述:补丁在修bug时,有时也会引入新的bug,假设有n(n<=20)个潜在的bug和m(m<=100)个补丁,每个补丁用两个长度为n的字符串表示,其中字符串的每个位置表示一个bug.第一个串表示打补丁之前的状态('-'表示在该位置不存在bug,'+'表示该位置必须存在bug,0表示无所谓),第二个串表示打补丁之后的状态('-'表示不存在,'+'表示存在,0表示不变).每个补丁都有一个执行时间,你的任务是用最少的时间把一个所有bug都存在的软件通过打补丁的方式变得没

UVa658 It&#39;s not a Bug, it&#39;s a Feature! (最短路,隐式图搜索)

链接:http://vjudge.net/problem/UVA-658 分析:Dijkstra求隐式图最短路. 1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 const int maxn = 20; 6 const int maxm = 100 + 5; 7 const int INF = 1000000000; 8 9 int n, m, t[maxm], vis[1 << max

UVA 658 It&#39;s not a Bug, it&#39;s a Feature! (单源最短路,dijkstra+优先队列,变形,经典)

题意:有n个bug,有m个补丁,每个补丁有一定的要求(比如某个bug必须存在,某个必须不存在,某些无所谓等等),打完出来后bug还可能变多了呢.但是打补丁是需要时间的,每个补丁耗时不同,那么问题来了:要打多久才能无bug?(同1补丁可重复打) 分析: n<=20,那么用位来表示bug的话有220=100万多一点.不用建图了,图实在太大了,用位图又不好玩.那么直接用隐式图搜索(在任意点,只要满足转移条件,任何状态都能转). 但是有没有可能每个状态都要搜1次啊?那可能是100万*100万啊,这样出题

POJ 1482 It&#39;s not a Bug, It&#39;s a Feature! 状压+spfa

http://poj.org/problem?id=1482 题意:先黑了一发程序猿,说软件总是有漏洞,然后打补丁拆了东墙补西墙.现在软件有不超过20个漏洞,有不超过100个补丁,每个补丁有运用条件(某些漏洞不能存在,某些漏洞必须存在)和作用效果(补漏洞,产生新漏洞),已经安装时间,开始有所有漏洞,问是否有一种安装补丁的顺序能填补所有漏洞,并求最短的时间. 分析:因为只有20个漏洞,所以可以状压,大概100万的级别,每位为0表示没有这个漏洞,为1表示有这个漏洞.记f[i]表示达到状态i所需最短时

UVa 658 - It&#39;s not a Bug, it&#39;s a Feature!(Dijkstra + 隐式图搜索)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=599 题意: 补丁在修正bug时,有时也会引入新的bug.假定有n(n≤20)个潜在bug和m(m≤100)个补丁,每个补丁用两个长度为n的字符串表示,其中字符串的每个位置表示一个bug.第一个串表示打补丁之前的状态("-"表示该bug必须不存在,"+&

Chrome 39 CSS3 渐变动画BUG

本来地图网页运行的好好的,突然从上周四(2014/11/27)开始,就开始接到 PM 和 QA 说网页崩溃的报告. 然后我就在本机重现,刚开始以为是有网页上有什么内存泄漏:可是都运行这么长时间了,并且 IE 和 Firefox 都没有出现过此问题,看来应该是和 chrome 有关系了.说归这样说,可是需要证据啊.于是就猜测是不是 Chrome 新自动升级引入的BUG,然后就开始了漫长的排查过程. 找了好几个同事来协助排查,也没有发现页面上有任何内存使用异常的现象:但是就 Chrome 进程内存却

linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol &amp;#39;[email&#160;protected]@GLIBC_2.2.5&amp;#39;

解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如以下错误: undefined reference to symbol '[email protected]@GLIBC_2.2.5'/lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command li

错误号码2003 Can&amp;#39;t connect to MySQL server &amp;#39;localhost&amp;#39; (0)

错误描写叙述 错误原因 近期,我一直都能够用SQLyog连接本地数据库,可是近几天却无法连接:而且一直都报上述错误,我查阅了非常多资料,发现有非常多中说法 总结一下 第一,MySQL中的my.ini出错: 第二.权限不够. 第三,可能是改动了MySQL自带的User表: 第四,MySQL服务停止了 解决的方法 针对上面几种原因,我也做了尝试,发现还是报这个错误: 后来,我将MySQL重装了,再次用SQLyog连接本地数据库,结果成功连接上了. 错误号码2003 Can't connect to