zoukankan      html  css  js  c++  java
  • uva 658 最短路

     

      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.
    

    Miguel Revilla
    2000-05-22
     
    2.256S险过。。。
    我用的是暴力方法:
    bug数最多为20,所以最多有2^20种状态,对于每个状态,用每个patch去转换它,直到达到所有可达新状态,这里每次转换类似加弧,
    弧值大小不一,所以可用优先队列/Dijkstra优化搜索。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    
    #define MAXN 1300000
    
    int n, m;
    
    struct patch{
        int time, base, intr;   //耗时,对当前状态要求,对后续状态的影响
        int fbase, fintr;       //用来标记是否受当前状态影响,是否影响后续状态
    }p[111];
    
    struct node{
        int u, d;
        bool operator < (const node &rhs)const{
            return d > rhs.d;
        }
    };
    
    int d[MAXN];
    bool done[MAXN];
    int Dijkstra(cint s, cint t, cint k){
        priority_queue<node> q;
        fill_n(d, k, INF);
        fill_n(done, k, false);
        q.push((node){s, 0});   d[s]=0;
        while(!q.empty()){
            node ut = q.top();   q.pop();
            int u = ut.u, td = ut.d;
            if(u == t) return d[t];
            if(done[u]) continue;       done[u]=true;
    
            for(int i=0; i<m; i++){
                int time = p[i].time, base = p[i].base, intr = p[i].intr,
                    fbase = p[i].fbase, fintr = p[i].fintr;
                bool f=true;
                for(int j=0; j<n; j++){
                    int flag = (1<<j) & fbase;
                    if(flag) continue;
                    if(((base>>j)&1) ^ ((u>>j)&1)){     //当前状态不满足条件
                        f = false;  break;
                    }
                }
    
                if(f){
                    int new_state = u;
                    for(int j=0; j<n; j++){
                        int flag = (1<<j) & fintr;
                        if(flag) continue;
                        if((1<<j) & intr) new_state |= (1<<j); //置1
                        else new_state &= (~(1<<j));           //置0
                    }
                    if(d[new_state] > d[u] + time){            //relaxation
                        d[new_state] = d[u] + time;
                        q.push((node){new_state, d[new_state]});
                    }
                }
            }
        }
        return -1;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int kase=1;
        while(scanf(" %d %d", &n, &m)==2 && (n || m)){
            int i, j;
            char s1[33], s2[33];
            for(i=0; i<m; i++){
                int &time=p[i].time, &sb=p[i].base=0, &si=p[i].intr=0;
                int &fb=p[i].fbase=0, &fi=p[i].fintr=0;
                scanf(" %d %s %s", &time, s1, s2);
                for(j=0; j<n; j++){
                    if(s1[j]=='+')      sb |= (1<<j);
                    else if(s1[j]=='0') fb |= (1<<j);
                    if(s2[j]=='+')      si |= (1<<j);
                    else if(s2[j]=='0') fi |= (1<<j);
                }
            }
            int ans = Dijkstra((1<<n)-1, 0, 1<<n);
            printf("Product %d
    ", kase++);
            if(ans<0) printf("Bugs cannot be fixed.
    
    ");
            else printf("Fastest sequence takes %d seconds.
    
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    多种方法求解八数码问题
    GridView编辑删除操作
    【翻译自mos文章】v$undostat视图没有依照每10分钟进行更新,v$undostat仅仅有1行(one rows)
    支持向量机通俗导论(理解SVM的三层境地)
    bootstrap课程13 bootstrap的官方文档中有一些控件的使用有bug,如何解决这个问题
    bootstrap课程12 滚动监听如何实现(bootstrap方式和自定义方式)
    bootstrap课程11 模态框如何使用
    bootstrap课程10 从外部引入视频到页面用什么标签
    bootstrap课程9 bootstrap如何实现动画加载进度条的效果
    bootstrap课程8 bootstrap导航条在不同设备上的显示效果如何
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3377204.html
Copyright © 2011-2022 走看看