• 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   Accepted: 466

    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

    好坑的一题 ,刚开始没有考虑到不同走的方法是不等价的,所以错了很多次,用个优先队列就可以了,直接上代码,但不知道,那些0m的是怎么搞的,我把能优化的都优化了啊,像什么二进制,我估计还有什么好的优化方法吧,等以后再看看
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<queue>
    using namespace std;
    int n,m,time[200],before[2][200],after[2][200],visit[2000000];
    struct bugtree{int time ,bugnum;
    bool operator <(const bugtree a)const //重载比较函数,为下面队作准备,也可以在外面
    {
        if(time>a.time)//这里是大于号,要小心这里和下面优先队列的排列顺序是有很大关系的!
        return true;
        else
        return false ;
    }
    
    };
    bool bugcan(int i,bugtree f)//判断是否符合第m种的操作
    {
        int ignore;
        ignore=f.bugnum&(~before[0][i]);//把不用考虑的全部置成0其它的不变
    //	printf("%d ignore
    ",ignore);
        if(ignore==before[1][i])//如果在不能有的位置上有1,就要返回假
        return true;
        return false ;
    }
    int bfs()
    {
        int i;
        bugtree temp,ss;
        priority_queue < bugtree > q;
        memset(visit,-1,sizeof(visit)); 
    	while(!q.empty())//清空
        q.pop();
        temp.time=0;
        temp.bugnum=(1<<n)-1;
    	//printf("temp %d
    ",temp.bugnum);
    	visit[temp.bugnum]=temp.time;
        q.push(temp);
    	while(!q.empty())
        {
            temp=q.top();
            q.pop();
            if(temp.bugnum==0)
            {
                return temp.time;//找到最小值,在这里判断会找到最小值
            }
            for(i=0;i<m;i++)
            {
    
                if(bugcan(i,temp))//满足第m条件
                {
    			
                    ss.time=temp.time+time[i];
                    ss.bugnum=(temp.bugnum|after[0][i])&after[1][i];
    			//	printf("%d i%d ss
    ",i,ss.bugnum);
                    if((visit[ss.bugnum]<0)||(ss.time<visit[ss.bugnum]))//未访问过,或者,时间更少,都加入队列
                    {
    				//	printf(" i%d  %di
    ",i,ss.bugnum);
                        visit[ss.bugnum]=ss.time;
                        q.push(ss);
                    }
    
                }
            }
     }
        return -1;//没有搜到
    }
    int main()
    {
    	int  i,j,re,tcase;
    	char c;tcase=1;char strtemp[200];
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    
    		if(n==0&&m==0)
    			break;
    		for(i=0;i<m;i++ )
    		{
    		    scanf("%d",&time[i]);
    		    getchar();
    		    before[0][i]=0;
    		    before[1][i]=0;
    		    after[0][i]=0;
    		    after[1][i]=0;
    		    for( j=0;j<n;j++)//是否能操作,如果把两者分开了,就可以全部用二进制进行操作,对于后面的搜索是非常有用的
    		    {
                    c=getchar();
                    before[0][i]=before[0][i]<<1;//是第i个,不能用j
                    before[1][i]=before[1][i]<<1;//在每一个循环都要左移,为了记录每一个位置的相应的变化
                    if(c=='0')
                    {
                        before[0][i]=before[0][i]|1;//用1来表示是进行操作
    					
                    }
                    else if(c=='-')//要将-和0分开,这是两种不同的操作
                    {
                       // before[1][i]=before[1][i];//用1来标记是否要这个条件
                    }
    				else if(c=='+')
    				{
    					before[1][i]=before[1][i]|1;	
    				}
    
    		    }
                getchar();//将中间的空格号收掉
                for( j=0;j<n;j++)//改掉bug
                {
                    c=getchar();
                    if(c=='+')
                    {
                        after[0][i]=after[0][i]<<1|1;//加用1或,不用管的地方用0
                        after[1][i]=after[1][i]<<1|1;
                    }
                    else if(c=='-')
                    {
                        after[0][i]=after[0][i]<<1;
                        after[1][i]=after[1][i]<<1;//减用0且,不用管的地方用1
                    }
                    else if(c=='0')
                    {
                        after[0][i]=after[0][i]<<1;
                        after[1][i]=(after[1][i]<<1)|1;
                    }
    
    
                }
                gets(strtemp);//将最后的回车收掉
    
    
    		}
    		//printf("%d %dafter 
    ",after[0][0],after[1][0]);
    		re=bfs();
    		printf("Product %d
    ",tcase++);
            if(re!=-1)
            {
                printf("Fastest sequence takes %d seconds.
    
    ",re);
            }
            else{
    
    			printf("Bugs cannot be fixed.
    
    ");
            }
    
    	}
    
    	return 0;
    }
    


  • 相关阅读:
    vector
    codeforces 1453D. Checkpoints
    [ICPC2019 WF]Hobson's Trains
    [ICPC2019 WF]Circular DNA
    计算几何板子
    CSP-S2020 贪吃蛇(洛谷民间数据)
    CSP-S2020 函数调用(洛谷民间数据)
    [NOI Online #3 提高组]魔法值
    [NOI Online #1 提高组]冒泡排序
    佳能m62套机5500 佳能EOS M50 M6 MARK2 II二代 最低到过5800
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3186870.html
Copyright © 2020-2023  润新知