POJ1273 dinic Pascal模板
之前写的其实不是dinic真不好意思……耽误大家了。现在重写了一个真正的dinic。 2013-05-13
program dinic; //Algorithm:Dinic for Network //Author:HT //Date:2013/05/10 Const inf=1000000; Type rec=record s,e,w,flow,next:longint; end; Var b,d,q:array[0..300] of longint; a:array[-1..450] of rec; v:array[0..300] of boolean; n,m,i,st,ed,ww,top,tar:longint; Function min(a,b:longint):longint;inline;begin if a<b then exit(a);exit(b); end; Procedure Add(st,ed,ww:longint);inline; begin inc(top); with a[top] do begin s:=st; e:=ed;w:=ww;next:=b[st]; end; b[st]:=top; end; function bfs:boolean; var head,tail,x,u:longint; y:rec; begin fillchar(v,sizeof(v),false); tail:=1;head:=0;d[st]:=1; v[st]:=true; q[1]:=st; while head<tail do begin inc(head); x:=q[head]; u:=b[x]; while u<>b[0] do begin y:=a[u]; if (not v[y.e]) and (y.flow<y.w) then begin v[y.e]:=true; d[y.e]:=d[x]+1; inc(tail); q[tail]:=y.e; end; u:=y.next; end; end; exit(v[tar]); end; Function Addflow(p,maxflow:longint):longint; var u,o:longint; y:rec; begin if (p=tar) or (maxflow=0) then exit(maxflow); addflow:=0; u:=b[p]; while u<>b[0] do begin y:=a[u]; if (d[y.e]=d[p]+1) and (y.flow<y.w) then begin o:=Addflow(y.e,min(maxflow,y.w-y.flow)); if o>0 then begin inc(a[u].flow,o); dec(a[u xor 1].flow,o); dec(maxflow,o); inc(addflow,o); if maxflow=0 then break; end; end; u:=y.next; end; end; function network:longint; begin network:=0; while bfs do begin inc(network,addflow(st,inf)); //writeln('network=',network); end; end; begin while not eof do begin fillchar(b,sizeof(b),$ff); fillchar(a,20*(2*m+2),0); readln(m,n); top:=-1; for i:=1 to m do begin readln(st,ed,ww); Add(st,ed,ww); Add(ed,st,0); end; st:=1;tar:=n; writeln(Network); end; end.