[Problem] uva658 It's not a bug, it's a feature!

Source: Internet
Author: User

Link: http://vjudge.net/problem/viewProblem.action? Id = 22169

Description: There are n vulnerabilities and M vulnerability repair methods. Each method takes a different time, and the shortest time is required to fix the vulnerability. The use of each method requires the distribution of the current vulnerability and can be repaired only when it meets the requirements. New vulnerabilities may be introduced.

Idea: single-source point Shortest Path

This question has been stuck for a long time because I don't know how to express the status. It was initially affected by '0' and felt that it had to be in triplicate, so it was very troublesome and wrong. Then, we thought we could use binary data to store the states of '+' and. I define two arrays. condition [I] [0] indicates the conditions to be met by the I method '+, condition [I] [1] indicates the condition to be met by the I method '-'. Operate [I] [0] and operate [I] [1] also indicate the repair means.

Determine whether the current status of cur can use the I Method: (you can give an example by yourself)

Cur & condition [I] [0]) = condition [I] [0]

((~ Cur) & condition [I] [1]) = condition [I] [1]

Use method I to fix the vulnerability:

Cur | = operate [I] [0];

Cur & = (~ Operate [I] [1]);

The problem is that there are too many States, or too many invalid states. How can we solve the storage problem? At this time, we will consider not storing edges, and follow the judgment results during the search, which is equivalent to dynamic searching. Then a BFS solves the problem.

 

My implementation:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 #define MaxM 120 7 #define MaxN 30 8 int Cost[MaxM],Condition[MaxM][5],Operate[MaxM][5]; 9 char s1[MaxN],s2[MaxN];10 bool vis[(1<<20)+20];11 int n,m,ans;12 bool Sol;13 typedef pair<int, int> p;14 struct cmp15 {16     bool operator()(p a,p b)17     {18         return a.first>b.first;19     }20 };21 priority_queue <p, vector<p>, cmp> q;22 inline void Clean()23 {24     memset(Condition,0,sizeof(Condition));25     memset(Operate,0,sizeof(Operate));26     Sol=false;27     memset(vis,false,sizeof(vis));28     while(!q.empty())29         q.pop();30 }31 void Bfs()32 {33     q.push(make_pair(0,(1<<n)-1));34     p u;35     int i,fi,Cur;36     while(!q.empty())37     {38         u=q.top();q.pop();39         fi=u.first;Cur=u.second;40         if(!Cur)41         {42             Sol=true;43             ans=fi;44             return;45         }46         if(vis[Cur])47             continue;48         vis[Cur]=true;49         for(i=1;i<=m;++i)50         {51             Cur=u.second;52             if((Cur&Condition[i][0])==Condition[i][0]&&((~Cur)&Condition[i][1])==Condition[i][1])53             {54                 Cur|=Operate[i][0];55                 Cur&=(~Operate[i][1]);56                 q.push(make_pair(fi+Cost[i],Cur));57             }58         }59     }60 }61 int main()62 {63     int i,j,t;64     for(t=1;;t++)65     {66         Clean();67         scanf("%d%d",&n,&m);68         if(n==0&&m==0)69             break;70         for(i=1;i<=m;++i)71         {72             scanf("%d%s%s",&Cost[i],s1,s2);73             for(j=0;j<n;++j)74             {75                 if(s1[j]==‘+‘)76                     Condition[i][0]|=(1<<j);77                 else if(s1[j]==‘-‘)78                     Condition[i][1]|=(1<<j);79             }80             for(j=0;j<n;++j)81             {82                 if(s2[j]==‘+‘)83                     Operate[i][0]|=(1<<j);84                 else if(s2[j]==‘-‘)85                     Operate[i][1]|=(1<<j);86             }87         }88         Bfs();89         printf("Product %d\n",t);90         if(Sol)91             printf("Fastest sequence takes %d seconds.\n\n",ans);92         else93             printf("Bugs cannot be fixed.\n\n");94     }95     return 0;96 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.