Bzoj 2330: [SCOI2011] Candy

Source: Internet
Author: User

2330: [SCOI2011] Candy time limit:10 Sec Memory limit:128 MB
submit:4112 solved:1264
[Submit] [Status] [Discuss] 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 -Line, which represents the relationship that these points need to satisfy, is 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 Output
11

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

Source

Day1

Differential constrained bare topic. You can poke here without a differential constraint.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineLL Long Long4 5Template <classT> InlinevoidRead (T &x) {6     Static CharC;7      while(!isdigit (c =GetChar ()));8      for(x =0; IsDigit (c); c = GetChar ()) x = x *Ten+ C- -;9 }Ten  One Const intMAXN = 1e5 +Ten; A  - structEdge {intTo, Next, W; } E[MAXN <<2]; -  the intN, K; - intST[MAXN]; - intF[MAXN], VIS[MAXN],inch[MAXN]; -queue<int>Q; +  -InlinevoidAddedge (intUintVintW) + { A     Static intCNT =0; atE[++CNT] =(Edge) {V, st[u], w}; -St[u] =CNT; - } -  - BOOLSPFA () - { inMemset (F,0xEF,sizeoff); -f[0] =0; toQ.push (0); +      while(Q.size ()) { -         intx =Q.front (); Q.pop (); theVIS[X] =0; *          for(inti = st[x]; I i =E[i].next) { $             intv =e[i].to;Panax Notoginseng             if(F[v] < f[x] +E[I].W) { -F[V] = F[x] +E[I].W; the                 if(inch[v] + + >N) { +                     return 0; A                 } the                 if(!Vis[v]) { +VIS[V] =1; - Q.push (v); $                 } $             } -         } -     } the     return 1; - }Wuyi  the intMain () - { Wu read (N); Read (K); -      for(inti =1; I <= K; ++i) { About         intK, A, B; $ Read ( k), read (a), read (b); -         if(k = =1) {Addedge (A, B,0); Addedge (b, A,0); } -         if(k = =2) {Addedge (A, B,1); } -         if(k = =3) {Addedge (b, A,0); } A         if(k = =4) {Addedge (b, A,1); } +         if(k = =5) {Addedge (A, B,0); } the     } -      for(inti = N; I --i) { $Addedge (0I1); the     } the     if(!SPFA ()) thePuts"-1"); the     Else { -LL ans =0; in          for(inti =1; I <= N; + + i) ans + =F[i]; theprintf"%lld\n", ans); the     } About}
View Code

Bzoj 2330: [SCOI2011] 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.