"Codevs1227" Check number 2 (fee flow)

Source: Internet
Author: User

Test instructions: give a matrix of n*n, each with a nonnegative integer Aij, (Aij <=)

Now starting from (a), you can go to the right or down, and finally arrive (N,n), each reached a lattice, the number of the lattice is taken out, the number of the lattice becomes 0,

This takes a total of k times, now requires K times to reach the number of squares and the largest.

n<=50,k<=10

Idea: Cost flow

Splitting each point into an out point and an in point (i,j,1..2), this idea is similar to the maximum flow

(i,j,1) (i,j,2) with two edges:

Capacity is 1, cost a[i,j]

Capacity is K, cost is 0

(i,j,2)-(i+1,j,1) with a capacity of k, the cost of 0 side (i,j+1) Similar

(n,n,2)->t with a flow of k, with a cost of 0 side limit traffic

Run the maximum cost of the maximum flow on the line, in fact, is SPFA triangle inequality change direction

1 varFanArray[1..200000] ofLongint;2Q:Array[0..20000] ofLongint;3Head,vet,next,len1,len2:Array[1..20000] ofLongint;4PreArray[1..20000,1..2] ofLongint;5InqArray[1..20000] ofBoolean;6Dis:Array[1..20000] ofLongint;7A:Array[1.. -,1.. -] ofLongint;8Num:Array[1.. -,1.. -,1..2] ofLongint;9 N,m,k1,i,j,k,tot,s,source,src:longint;Ten Ans:int64; One  A functionmin (x,y:longint): Longint; - begin -  ifX<y Thenexit (x); the exit (y); - End; -  - procedureAdd (a,b,c,d:longint); + begin - Inc (TOT); +next[tot]:=Head[a]; Avet[tot]:=b; atlen1[tot]:=C; -len2[tot]:=D; -head[a]:=tot; -  - Inc (TOT); -next[tot]:=Head[b]; invet[tot]:=A; -len1[tot]:=0; tolen2[tot]:=-D; +head[b]:=tot; - End; the  * functionSpfa:boolean; $ varU,t,w,i,e,v:longint;Panax Notoginseng begin -   fori:=1  toS Do the  begin +dis[i]:=-Maxlongint; Ainq[i]:=false; the  End; +t:=0; w:=1; q[1]:=source; Inq[source]:=true; dis[source]:=0; -   whileT<w Do $  begin $Inc (T); U:=q[tMoD(s+5)]; -inq[u]:=false; -e:=Head[u]; the    whileE<>0  Do -   beginWuyiv:=Vet[e]; the    if(len1[e]>0) and(Dis[u]+len2[e]>dis[v]) Then -    begin WuPre[v,1]:=u; Pre[v,2]:=e; -dis[v]:=dis[u]+Len2[e]; About     if  notINQ[V] Then $     begin -Inc (W); Q[wMoD(s+5)]:=v; -inq[v]:=true; -     End; A    End; +e:=Next[e]; the   End; -  End; $  ifDis[src]=-maxlongint Thenexit (False) the   Elseexit (true); the End; the  the procedureMCF; - varK,e:longint; in T:int64; the begin theK:=SRC; t:=Maxlongint; About   whileK<>source Do the  begin theE:=pre[k,2]; K:=pre[k,1]; thet:=min (t,len1[e]); +  End; -k:=src; the   whileK<>source DoBayi  begin theE:=pre[k,2]; thelen1[e]:=len1[e]-T; -len1[fan[e]]:=len1[fan[e]]+T; -ans:=ans+t*Len2[e]; theK:=pre[k,1]; the  End; the End; the  - begin theAssign (input,'codevs1227.in'); Reset (input); theAssign (output,'Codevs1227.out'); Rewrite (output); the readln (N,K1);94   fori:=1  to 200000  Do the   ifIMoD 2=1  Thenfan[i]:=i+1 the    Elsefan[i]:=i-1; the   fori:=1  toN Do98    forj:=1  toN Doread (a[i,j]); About   fori:=1  toN Do -    forj:=1  toN Do101     fork:=1  to 2  Do102    begin103Inc (s); num[i,j,k]:=s;104    End; the   fori:=1  toN Do106    forj:=1  toN Do107   begin108Add (Num[i,j,1],num[i,j,2],1, A[i,j]);109Add (Num[i,j,1],num[i,j,2],K1,0); the    ifj+1<=n ThenAdd (Num[i,j,2],num[i,j+1,1],K1,0);111    ifi+1<=n ThenAdd (Num[i,j,2],num[i+1J1],K1,0); the   End;113source:=1; src:=s+1; Inc (s); theAdd (Num[n,n,2],SRC,K1,0); the   whileSpfa DoMCF; the writeln (ans);117 close (input);118 close (output);119 End.

"Codevs1227" Check number 2 (fee flow)

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.