Bzoj 2330: [SCOI2011] candy [differential constraint system] "Learning notes"

Source: Internet
Author: User

2330: [SCOI2011] Candy time limit:10 Sec Memory limit:128 MB
submit:5395 solved:1750
[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.

"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

The following own understanding

A set of linear programming problems, similar to the trigonometric inequalities in the shortest circuit, solved by the shortest path algorithm

A-b<=c----> D[v]<=d[u]+c

Objective function The shortest way ah, what do you mean, the target function?

However, after I have done so, it has been a sample!

Suddenly found that they all ran the longest way

Originally:

In a differential constraint system, the maximum value of each variable is calculated (the value is relative, and the difference constraint system is determined by the value of one of the variables, such as D[s])

is to use the shortest circuit according to the <= map

Minimum value is the longest road, according to the >= to build the map

More formalized:

①: For the difference inequality, a-B <= C, build a C-to-a weight of the side, the shortest way, get the maximum value
②: For inequality-A-B >= C, build a C-to-a-weight value for the side of a, ask for the longest road, get the minimum value
③: There is no solution to the existence of negative rings.
④: Cannot find the shortest circuit (dist[] is not updated) is the arbitrary solution

Why is the shortest circuit the minimum value?

Introduction to recall algorithm 504 pages when the shortest path is converted into a linear programming form, d[t] is maximized, because this linear programming is to satisfy the greatest d[t of those inequalities], because this problem has practical significance if the smallest words d[t] take-inf, obviously this is wrong

See another idea on the Web: "The shortest path is constrained by infinity, so it must be the maximum value"

There is also a problem with the implementation:

Is it necessary to add a super source to avoid non-connectivity?

No, I remember there is a study questions in the introduction of algorithms. As with negative loops, add all the points to the queue d[i]=0 .

For the subject, because at least one candy, so d[i]=1

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong Longll;Const intn=1e5+5, m=1e5+5, inf=1e9;inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,x,a,b,s;structedge{intV,ne; DoubleW;} E[m<<1];intH[n],cnt=0; inlinevoidInsintUintVintW) {CNT++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT;}intQ[n],head,tail,inq[n],num[n],d[n];inlinevoidLopint&AMP;X) {if(x==n) x=1;}BOOLSPFA (ints) {     for(intI=1; i<=n;i++) d[i]=-INF; Head=tail=0; memset (INQ,0,sizeof(INQ)); memset (num,0,sizeof(num));  for(intI=1; i<=n;i++) q[tail++]=i,inq[i]=1, d[i]=1;  while(head!=tail) {        intu=q[head++];inq[u]=0; Lop (head);  for(intI=h[u];i;i=e[i].ne) {            intv=e[i].v,w=E[I].W; if(d[v]<d[u]+W) {D[v]=d[u]+W; if(!Inq[v]) {Inq[v]=1, q[tail++]=v,lop (tail); if(++num[v]>n)return true; }            }        }    }    return false;}intMain () {n=read (); M=read (); s=0;  for(intI=1; i<=m;i++) {x=read (); A=read (); b=read (); if(x==1) ins (A, B,0), Ins (B,a,0); Else if(x==2){if(a==b) {Puts ("-1");return 0;} Ins (A, B,1);} Else if(x==3) Ins (B,a,0); Else if(x==4){if(a==b) {Puts ("-1");return 0;} Ins (B,a,1);} ElseIns (A, B,0); }    if(SPFA (s)) puts ("-1"); Else{ll ans=0;  for(intI=1; i<=n;i++) ans+=D[i]; printf ("%lld", ans); }    }

Bzoj 2330: [SCOI2011] candy [differential constraint system] "Learning notes"

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.