UVa 658 - It's not a Bug, it's a Feature!(Dijkstra算法)

4 篇文章 0 订阅
4 篇文章 0 订阅

UVa的题目地址如下:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=599


题目的描述为:

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.


题目是一个隐式图求最短路的问题,要求得出从全1到全0状态的最时间(为0表示该处没有BUG,为1则表示该处有BUG)。注意到BUG的数量最多为20,用二进制来表示状态是靠谱的。另外,补丁的数量高达100,但是其实有很多状态是无论如何也达不到的,故我们没必要一开始就把图建好,否则会导致内存溢出。另外,对于打补丁的先决条件00++--,我们可以分解为op1=001100,op2=111100,若当前状态s&op1==op1并且s|op2==op2,我们就认为可以打补丁了。最后,记得在每个case后面打一个空行。提供Accepted的代码如下:


/* 
 * File:   main.cpp
 * Author: seedeed
 *
 * Created on 2014年11月30日, 下午12:46
 */

#include <cstdio>
#include <queue>
#include <vector>
#define MAXS ((1 << 20) + 20)
#define INF ((1 << 31) - 1)

using namespace std;

class Patch {
public:
    int second;
    int op1; //对应必须为+才能打,执行&1
    int op2; //对应必须为-才能打,执行|0
    int op3; //打之后的+,执行|1
    int op4; //打之后的-,执行&0
    Patch(char* pre, char* post, int countOfBugs, int second) {
        this->op1 = 0xffffffff >> (32 - countOfBugs);
        this->op2 = (1 << 31) >> (31 - countOfBugs);
        this->second = second;
        for (int i = 0; i < countOfBugs; i++) {
            if (pre[i] == '+') {
                this->op2 |= 1 << i;
            } else if (pre[i] == '-') {
                this->op1 &= ~(1 << i);
            } else {
                this->op1 &= ~(1 << i);
                this->op2 |= 1 << i;
            }
        }
        this->op3 = 0;
        this->op4 = 0xffffffff;
        for (int i = 0; i < countOfBugs; i++) {
            if (post[i] == '+') {
                this->op3 |= 1 << i;
            } else if(post[i] == '-') {
                this->op4 &= ~(1 << i);
            }
        }
    }
    
    bool apply(int& status) {
        if ((status & op1) == op1 && (status | op2) == op2) {//可以打补丁
            status |= op3;
            status &= op4;
            return true;
        }
        return false;
    }
};

struct Node {
    int totalSeconds;
    int status;
    
    Node(int status, int totalSecnods = 0) : totalSeconds(totalSecnods), status(status) {}
    
    bool operator < (const Node& another) const {
        return totalSeconds > another.totalSeconds;
    }
};

int seconds[MAXS];

int main() {
    //freopen("c:\\test.txt","r",stdin);
    int countOfBugs, countOfPatches, countOfCases = 1, second;
    priority_queue<Node> pq;
    vector<Patch> patches;
    char pre[30], post[30];
    while (scanf("%d%d", &countOfBugs, &countOfPatches) > 0 && countOfBugs && countOfPatches) {
        bool found = false;
        for (int i = 0; i <MAXS; i++) {
            seconds[i] = INF;
        }
        for (int i = 0; i < countOfPatches; i++) {
            scanf("%d%s%s", &second, pre, post);
            patches.push_back(Patch(pre, post, countOfBugs, second));
        }
        int status = 0xffffffff >> (32 - countOfBugs);
        seconds[status] = 0;
        pq.push(Node(status));
        Node node(0);
        //dijkstra算法
        while (!pq.empty()) {
            node = pq.top();
            pq.pop();
            if(node.status == 0) {
                found = true;
                break;
            }
            if (seconds[node.status] != node.totalSeconds) {
                continue;
            }
            for (int i = 0; i < patches.size(); i++) {
                status = node.status;
                if (patches[i].apply(status) && seconds[status] > node.totalSeconds + patches[i].second) {
                    seconds[status] = node.totalSeconds + patches[i].second;
                    pq.push(Node(status, seconds[status]));
                }
            }
        }
        printf("Product %d\n", countOfCases++);
        if (found) {
            printf("Fastest sequence takes %d seconds.\n", seconds[0]);
        } else {
            puts("Bugs cannot be fixed.");
        }
        puts("");
        while (!pq.empty()) {
            pq.pop();
        }
        patches.clear();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值