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 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
Differential constraints according to my previous writing, are x-y<=z to build a Y to x length of the Edge of Z, Huang long code tells me Y-x>=z can build a x-to-y length of the z-side of the additional SPFA of greater than less than the number of changes can run the longest road ...
1#include <cstdio>2#include <vector>3#include <cstring>4 using namespacestd;5 Const intn=1000005;6 intcs[n],head[4*N],dis[n],b[n];7 BOOLSs[n],flag;8 intn,k,sum,cnt;9 Long Longans;Ten structee{intTo,next,v;} e[n*4]; One voidAddintUintVintW) { Ae[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=W; - } - the voidSPFA (ints) { - intH=0, t=1; -b[t]=s;dis[s]=0; cs[s]++; - while(h!=t) { + intNOW=B[++H];if(h==n) h=0; - for(intL=head[now];l;l=E[l].next) { + intv=e[l].to; A if(dis[now]+e[l].v>Dis[v]) { atdis[v]=dis[now]+e[l].v; - if(!Ss[v]) { -b[++t]=v; - if(t==n) t=0; -ss[v]=1; -cs[v]++; in if(cs[v]>=n) {flag=1;return;} - } to } + } -ss[now]=0; the } * } $ Panax Notoginseng intMain () { -scanf"%d%d",&n,&k); the intu,opt,v; + for(intI=1; i<=k;i++){ Ascanf"%d%d%d",&opt,&u,&v); the Switch(opt) { + Case 1: Add (U,v,0); Add (V,u,0); Break; - Case 2:if(U==V) {printf ("-1");return 0;} Add (U,v,1); Break;//there must be a special sentence, otherwise it would be T, but I don't know why, it could be the data problem $ Case 3: Add (V,u,0); Break; $ Case 4:if(U==V) {printf ("-1");return 0;} Add (V,u,1); Break; - Case 5: Add (U,v,0); Break; - } the } - for(intI=n;i>0; i--) Add (0I1);WuyiSPFA (0); the if(flag) { -printf"-1");return 0; Wu } - for(intI=1; i<=n;i++) ans+=Dis[i]; Aboutprintf"%lld", ans); $}
"BZOJ2330" [SCOI2011] Candy