Minimum cost flow SPFA algorithm template (pascal)

Source: Internet
Author: User

Previously written, the current code of the wind and some changes in the past, mainly by using arrays to simulate the adjacency table map, previously used as a pointer to the MAP.

Previous posts:http://www.cnblogs.com/Currier/p/6387732.html

The valley can be Evaluated.

Portal:https://www.luogu.org/problem/show?pid=3381

1  programRRR (input,output);2 Const3inf=123456789;4 type5Etype=Record6 t,c,w,next,rev:longint;7   End;8 var9E:Array[0..100010] ofetype;TenA,fre,frv,q,dis:Array[0..5050] oflongint; oneInqArray[0..5050] ofboolean; a n,m,s,t,i,j,x,y,c,w,max,ans,cnt,f:longint; - procedureAdd (x,y,c,w:longint); - begin theInc (cnt); E[cnt].t:=y;e[cnt].c:=c;e[cnt].w:=w;e[cnt].next:=a[x];a[x]:=cnt; - End; - functionmin (a,b:longint): longint; - begin +    ifA<b thenExit (a)ElseExit (b); - End; + procedurespfa; a var at h,t:longint; - begin -     fori:=1  toN dodis[i]:=inf; - Fillchar (inq,sizeof (inq), false); -h:=0; t:=1; q[1]:=s;dis[s]:=0; inq[s]:=true; -     whileH<>t do in       begin -Inc (h);ifH> the  thenh:=1; toi:=a[q[h]]; +           whileI<>0  do -             begin the                if(e[i].c>0) and(dis[q[h]]+e[i].w<dis[e[i].t]) then *                   begin $dis[e[i].t]:=dis[q[h]]+e[i].w;Panax Notoginsengfrv[e[i].t]:=q[h];fre[e[i].t]:=i; -                      if  notinq[e[i].t] then the                         begin +Inc (t);ifT> the  thent:=1; aq[t]:=e[i].t;inq[e[i].t]:=true; the                         End; +                   End; -i:=e[i].next; $             End; $inq[q[h]]:=false; -       End; - End; the begin -Assign (input,'r.in'); Assign (output,'R.out'); reset (input); rewrite (output) ;Wuyi readln (n,m,s,t); theFillchar (a,sizeof (a),0); cnt:=0; -     fori:=1  toM do wu       begin - readln (x,y,c,w); aboutAdd (x,y,c,w); e[cnt].rev:=cnt+1; $Add (y,x,0,-w); e[cnt].rev:=cnt-1; -       End; -max:=0; ans:=0; -     whileTrue do a       begin + spfa; the          ifDis[t]=inf thenbreak ; -j:=t;f:=inf; $           whileJ<>s do beginF:=min (f,e[fre[j]].c); j:=frv[j];End; themax:=max+f; thej:=t;w:=0; the           whileJ<>s do beginW:=w+e[fre[j]].w;dec (e[fre[j]].c,f); Inc (e[e[fre[j]].rev].c,f); j:=frv[j];End; theans:=ans+w*f; -       End; inWrite (max,' ', ans); the Close (input); Close (output); the End.

Minimum cost flow SPFA algorithm template (pascal)

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.