Test instructions: There are in the kindergartenNa little friend,LXHGWWThe teacher now wants to assign sweets to the children and ask every child to get the sweets. But the children also have jealousy, will always put forward some requirements, such as Xiao Ming does not want to small red points to the candy more than his, so in the allocation of sweets,LXHGWWneed to meet the children'sKa requirement. Kindergarten sweets are always limited,LXHGWWto know how many sweets he needs at least to make it possible for every child to get a candy and meet all the demands of the children.
Analysis: A very obvious differential constraint system.
Because it is the minimum value, it is necessary to convert all inequalities into x-y>=k form and then even a y->x with a weighted value of K.
The following four operations are simple, the key is the first operation.
Then we can convert the a=b into a-b>=0 and b-a>=0 and run the longest road to the last statistic.
There is the problem of judging the negative power (shortest way) or the right (the longest road) loop, if a point is relaxed n times or more is a loop.
There is a magical place to add the starting point to all points of the side of the time to pour or else will be T (I do not build hair)
Code:
Const
maxn=100005; Maxm=100005;var STATE,D,LAST,TIME:ARRAY[0..MAXN] of Longint; V:ARRAY[0..MAXN] of Boolean; Side:array[1..maxm*3] of record x,y,z,next:longint; End N,m,e:longint;procedure Add (X,y,z:longint); Begin Inc (E); Side[e].x:=x; Side[e].y:=y; Side[e].z:=z; SIDE[E].NEXT:=LAST[X]; Last[x]:=e;end;procedure Init;var x,y,z,i:longint;begin readln (n,m); For I:=1 to M do begin READLN (z,x,y); Case Z of 1:begin Add (x,y,0); Add (y,x,0); End 2:add (x,y,1); 3:add (y,x,0); 4:add (y,x,1); 5:add (x,y,0); End End For I:=n Downto 1 do Add (0,i,1); end;procedure Spfa;var Head,tail,i,u:longint; Ans:int64;begin for I:=1 to E does with Side[i] does if (x=y) and (z>0) THEN begin Writeln (-1); Exit End head:=0; Tail:=1; state[1]:=0; Time[0]:=1; Fillchar (v,sizeof (v), true); V[0]:=false; Repeat Inc (head); If Head>n+1 then head:=1; U:=state[head]; I:=last[u]; While I>0 does with Side[i] do bEgin if D[X]+Z>D[Y] THEN BEGIN Inc (Time[y]); If Time[y]>n then BEGIN Writeln (-1); Exit End D[y]:=d[x]+z; If v[y] then begin v[y]:=false; Inc (tail); If Tail>n+1 then tail:=1; State[tail]:=y; End End I:=next; End V[u]:=true; Until Head=tail; ans:=0; For I:=1 to n do ans:=ans+d[i]; Writeln (ans); End;begin init; Spfa;end.
Bzoj 2330: [SCOI2011] candy Differential restraint system