UVA_658It's not a Bug, it's a Feature! (二进制+隐式图最短路)

9 篇文章 0 订阅

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

;UVA658


Intput

    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,然后每个补丁有对应的可以打补丁的串,000无所谓,-表示这个地方的bug已经被清除,+表示还有bug,


发现可以用二进制来表示他的状态,每个补丁用四个数来表示状态,-+ 分别用两个数来表示,如果-则这一位为1,经过一番推理发现,可以符合bug的是,| 以后比他原来还小的数,就比如010 可以用在000或者010  ,比如101可以用在000,001,100,101,经过|  运算可以进行判断,但是每次打完补丁之后的状态,不仅仅是|就可以了,因为还要考虑+的位置,这个位置是不能出现1也就-的,而~然后&操作可以很好的体现这一过程;

然后利用隐式图的思想,进行从+++ => -- 的路线,


在这个地方wa了好几次,他其实是一个图,到达每种状态的时间是需要更新最下值的,而有些走过的状态就不需要再一次进行重复计算。

综上,得到如下代码:

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>

using namespace std;
const int maxn=100+5;
const int maxx=(1<<20)+5;
const int inf=0x3f3f3f3f;
struct node
{
    int t;
    int f1,z1,f2,z2;
    node(int tt=0,int ff1=0,int zz1=0,int ff2=0,int zz2=0):t(tt),f1(ff1),f2(ff2),z1(zz1),z2(zz2){}
    bool operator<(const node &s)const
    {
        return t>s.t;
    }
};

node s[maxn];
int n,m;
int bf,bz;
int af,az;
map< pair<int,int> ,int> mp;
int dis[maxx];
int bfs()
{
    priority_queue<node> que;
    while(que.size())
       que.pop();
   que.push(node(0,0,0,af,az));
   int ka=0;
   dis[az]=0;
    while(que.size())
    {
        node a=que.top();
        que.pop();
        int z=a.z2,f=a.f2;
        int t=a.t;

        //cout<<"z= "<<z<<" f="<<f<<" t="<<t<<":   "<<(z&f)<<endl;;
        if(z==bz&&f==bf)
        {
            return t;
        }
        if(mp[make_pair(z,f)])
          continue;
         mp[make_pair(z,f)]=1;
         ka=1;

        for(int i=0;i<m;i++)
        {
            if((z|s[i].z1)<=z&&(f|s[i].f1)<=f)
            {
                int zz=((z&(~s[i].f2))|s[i].z2);
                int ff=((f&(~s[i].z2))|s[i].f2);
//                if(zz==bz&&ff==bf)     //不一定是最少
//                {
//                    return t+s[i].t;
//                }
                //cout<<" i="<<i<<" ,";
                if(dis[zz]>t+s[i].t)
                    dis[zz]=t+s[i].t;
                que.push(node(dis[zz],0,0,ff,zz));
            }
        }
       // cout<<endl;
    }
    return -1;
}
int main()
{
    // freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    int ka=0;
    while(cin>>n>>m&&(n&&m))
    {
        bf=(1<<n)-1;
        bz=0;
        af=0;
        az=bf;
        mp.clear();
        memset(s,0,sizeof(s));
        memset(dis,inf,sizeof(dis));
        for(int i=0;i<m;i++)
        {
            cin>>s[i].t;
            string a,b;
            cin>>a>>b;
            for(int j=n-1,jj=0;j>=0;j--,jj++)
            {
                if(a[j]=='-')
                {
                    s[i].f1=(s[i].f1|(1<<jj));
                }
                if(a[j]=='+')
                {
                    s[i].z1=(s[i].z1|(1<<jj));
                }
                if(b[j]=='-')
                {
                    s[i].f2=(s[i].f2|(1<<jj));
                }
                if(b[j]=='+')
                {
                    s[i].z2=(s[i].z2|(1<<jj));
                }
            }

        }
        int ans=bfs();
        if(ans==-1)
        {
            printf("Product %d\nBugs cannot be fixed.\n",++ka);
        }
        else
        {
            printf("Product %d\nFastest sequence takes %d seconds.\n",++ka,ans);
        }
        printf("\n");
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值