(bzoj2330) Candy

Source: Internet
Author: User

2330: [SCOI2011] Candy time limit:10 Sec Memory limit:128 MB
Description

There are N Children in kindergarten,lxhgww teacher now want to give these children to assign sweets, ask each child to be divided into sweets. But the children also have jealousy, will always put forward some requirements, such as Xiao Ming does not want to small red to the candy more than his, so in the distribution of sweets,lxhgww need to meet the children's K requirements. Kindergarten sweets are always limited,LXHGWW want to know how many sweets he needs at least, in order to make every child can be divided into sweets, and meet all the requirements of children.

Input

The first line of input is two integers N,K.

The next K lines, which represent the relationships that these points need to satisfy, are 3 digitsper line,X,A , B .

If x=1, said that the first child of the candy must be the same as the second child of the candy as much;

If the x=2, said that the first child of the candy must be less than the sweets of the second child ;

If x=3, said that the first child of the candy must not be less than the sweets of the children of the first B ;

If the x=4, said that the first child of the candy must be more than the second child of the candy;

If the x=5, said that the first child of the candy must not be more than the second child of the candy;

Output

Output line, indicating that lxhgww teacher needs at least the number of sweets to prepare, if not meet all the requirements of children, output -1.

Sample Input5 7

1 1 2

2 3 2

4 4 1

3 4 5

5 4 5

2 3 5

4 5 1
Sample Output11
HINT

"Data Range"

For 30% of data, ensure n<=100

For 100% of data, ensure n<=100000

For all data, ensure k<=100000,1<=x<=5,1<=a, b<=n

This problem is very simple SPFA (note that this is to use SPFA to find the maximum path) + differential constraints, the data will have pits.

OK, here are some things you might use:

When X=1, we can assign the weight of AB two children to 0.

When the x=2, there may be a hole in the data is a children and B children are the same person, here to Special award

Else we can take a road with a to B weight of 1, try not to deal with negative numbers.

When the x=3, because said as little as possible to consume sweets, so we can treat not less than as equals to deal with, is the assignment value of 0;

The remainder of the x=4~5 is likewise omitted.

Also note that when using a differential constraint system, you have to chain from N to one, otherwise the tle is at your own risk (I don't know why, the chain will be tle)

Finished, the following sticky code.

1#include <cstdio>2 #defineMAXN 2000053 structnode{intNext,aim,lon;};4Node e[3*MAXN];5 BOOLVIS[MAXN];6 intQUEUE[MAXN],TIME[MAXN],POINT,HEAD[MAXN],D[MAXN],T[MAXN];7 intn,k;8 Long Longans;9 voidAddintXintYintz)Ten { Onepoint++; Ae[point].next=Head[x]; -head[x]=Point ; -e[point].aim=y; thee[point].lon=Z; - } - BOOLSPFA () - { +  intHead=1, tail=2; -vis[0]=true; +t[0]++; A   while(head!=tail) at  { -  intu=Queue[head]; -    for(inti=head[u];i!=0; i=e[i].next) -   { -       intv=E[i].aim; -    if(d[v]<d[u]+E[i].lon) in     { -d[v]=d[u]+E[i].lon; to     if(Vis[v])Continue; +     if(++t[v]>n)return false; -vis[v]=true; thequeue[tail]=v; *tail++; $     if(TAIL==MAXN) tail=1;Panax Notoginseng     } -     }   thevis[u]=false; +head++; A if(HEAD==MAXN) Head=1;  the  } +  return true; - } $ intMain () $ { -scanf"%d%d",&n,&k); -   for(intI=1; i<=k;i++) the  { -      intx,a,b;Wuyiscanf"%d%d%d",&x,&a,&b); the   if(x==1) {Add (A, B,0); Add (B,a,0);} -   Else if(x==2) Wu   { -       if(a==b) {printf ("-1");return 0;} About   ElseAdd (A, B,1); $   } -   Else if(x==3) Add (B,a,0); -   Else if(x==4){ -       if(a==b) {printf ("-1");return 0;} A   ElseAdd (B,a,1); +   } the   Else if(x==5) Add (A, B,0); -  } $   for(inti=n;i>=1; i--) theAdd0I1); the  if(!SPFA ()) the{printf ("-1");return 0;} the Else  - { in   for(intI=1; i<=n;i++) theans+=D[i]; the } Aboutprintf"%lld", ans); the return 0; the}
View Code

(bzoj2330) Candy

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.