Codevs 2594 antidote or poison?

Source: Internet
Author: User

2594 antidote or poison?http://codevs.cn/problem/2594/Topic Description Description

Smart developed a cure for all kinds of symptoms, but he was careless, each medicine is a little bit wrong with a little raw material, so these drugs can be cured of certain conditions at the same time and make people suffer from some other conditions (you may ask that ...) Is that the antidote or the poison? ) ..., after smart efforts, finally understand the specific performance of each drug, he will be cured of each medicine and can make a list of symptoms of the disease to you, and then you have to follow this list to find a cure all the symptoms of the minimum pharmaceutical combination ... By the way, the number of illnesses is not more than 10, and his medicine is not finished, that is, each agent can be reused.

Enter a description Input Description

The first line in your list is the total number of illnesses N (1≤n≤10). The second line is the kind of the medicament m (0<m≤100).

The following are m lines, each line has n digits separated by a space, the file of the nth number of line i+2, if the number of J is 1, it means that the first drug can cure the disease j (if the disease is cured, no such disease is not affected), if 0 means no effect, If 1 means that it can cause the disease (no disease, no illness). Any two kinds of properties are different for smart-based drugs.

Output description Output Description

You just have to output the minimum number of potions you can, in fact, you may have exhausted all the drugs and can not cure all the disease, so you just output "the patient will be dead." You can do it.

Sample input Sample Input

3

2

1 0 1

-1 1 0

Sample output Sample Output

2

Data range and Tips Data Size & Hint

1≤n≤10

0<m≤100

The problem can be turned into the shortest path.

We have no cure for the first disease of 1, cured for 0. Example: Binary state 100 indicates that the 1th symptom was cured, and the 2nd and 3 symptoms were not cured and converted to decimal 4.

The initial state is a binary state of N 1, converted to a decimal 2^n-1, the target state is 0. All the states that go through the initial state to the target State are considered as points, and the Benquan of all sides is 1.

Then the title translates to: The shortest path from state 2^n-1 to state 0.

Basic algorithm: SPFA

Custom functions required for solving problems: decimal and binary conversions

Decimal Turn binary:

Take 10 as an example:

10%2= 0 , 10/2=5;

5%2=1, 5/2=2;

2%2=0, 2/2=1;

1%2=1, 1/2=0; end

So for 1010

Binary goto decimal:

Take 1010 as an example:

equals 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3=10

#include <cstdio>#include<iostream>#include<cmath>using namespacestd;intn,m;intHead=0, tail=0;//I'm using a left-right open interval, that is, the interval range is [HEAD,TAIL-1]intl[ One];//binary State Storage arrayinta[101][ One];//A[i][j] denotes the effect of the first I medicament on symptom JBOOLt[2050];//decimal sentence weight arraystructnode{intW,sum;//W Current state, decimal, sum to reach state W required number of potions};node q[10001];//Queuenode now;intChangeints) {    if(!s)return 0; returnL[s]+change (S-1)*Ten;}intWorkintDintG//d: Type D medicament, G: Status before using the D potion, decimal{     for(inti=n;i;i--)//Converts the decimal g to binary, and a single one exists in the L array{L[i]=g%2; G/=2; }     for(intI=1; i<=n;i++)//modification of the status of the medicament of the first D    {        if(a[d][i]==1) l[i]=0; Else if(a[d][i]==-1) l[i]=1; }    intEr=change (n);//because the number in the L array is a single deposit, this sentence combines the numbers in the l[] array. Example: l[1]=1,l[2]=0;er=10    intk=0, u=0;//Binary er into decimal k     while(er) {inty=er%2; K+=y*pow (2, u); U++; ER/=Ten; }    returnK;}voidPush (node cur) {Q[tail++]=cur;}voidpop () {head++;}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;i++)      for(intj=1; j<=n;j++) scanf ("%d",&A[i][j]); Q[TAIL].W=pow (2, N)-1;//initial State n 1 represents the binary number, converted to decimal as 2^n-1q[tail].sum=0;//The initial use of any potiont[q[0].w]=true; Tail++;//Queue up, tail pointer back move     while(Head<tail)//the queue is not empty    {         for(intI=1; i<=m;i++)//Enumerate each potion{ Now=q[head];//Take out the first team            intH=work (I,NOW.W);//before using the first medicament, the state is NOW.W, after the use of the first medicament, the state is H            if(!h)//Target State (cure){printf ("%d", now.sum+1);//since the value of now is the value before the medicament I, and the H update is used after the medicament I, so to +1                return 0; }             if(!t[h])//is not a target state and the status H has not occurred before. Because SPFA is a wide search, if the state H has been present before, it has been in the queue before{now.sum++;//number of potions +1Now.w=h;//State TransitionsPush (now);//Queuet[h]=true;//status H marked as already present}} pop ();//team first Out team} printf ("The patient would be dead.");//If there is no state in the queue, but there is no end to the program, there is no cure}

Codevs 2594 antidote or poison?

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.