首页 > 代码库 > DINIC网络流+当前弧优化
DINIC网络流+当前弧优化
const inf=1000000000; type rec=record s,e,w,next:longint; end; var b,bb,d,q:array[-1..300001] of longint; a:array[-1..1000001] of rec; e:array[0..300001,1..3]of longint; n,m,i,j,k,l,st,ed,ww,top,ans,nn,dd,x:longint; function min(aa,bb:longint):longint; begin if aa<bb then exit(aa);exit(bb); end; procedure add(st,ed,ww:longint); begin inc(top); a[top].s:=st; a[top].e:=ed; a[top].w:=ww; a[top].next:=b[st]; b[st]:=top; end; function bfs:boolean; var head,tail,x,u,i:longint; y:rec; begin for i:=1 to n do d[i]:=-1; tail:=1; head:=0; d[st]:=0; q[1]:=st; while head<tail do begin inc(head); x:=q[head]; u:=b[x]; while u>0 do begin y:=a[u]; if(d[y.e]=-1)and(y.w>0)then begin d[y.e]:=d[x]+1; inc(tail); q[tail]:=y.e; end; u:=y.next; end; end; if d[ed]=-1 then exit(false); exit(true); end; function addflow(p,maxflow:longint):longint; var o,u:longint; y:rec; begin if(p=ed)or(maxflow=0)then exit(maxflow); addflow:=0; u:=bb[p]; while u>0 do begin y:=a[u]; if(d[y.e]=d[p]+1)and(y.w>0)then begin o:=addflow(y.e,min(maxflow,y.w)); if o>0 then begin dec(a[u].w,o); if a[u].w>0 then bb[p]:=u; inc(a[u xor 1].w,o); dec(maxflow,o); inc(addflow,o); if maxflow=0 then break; end; end; u:=y.next; end; if addflow=0 then d[p]:=-1; end; function network:longint; var i:longint; begin network:=0; while bfs do begin for i:=st to ed do bb[i]:=b[i]; inc(network,addflow(st,inf)); end; end; begin readln(n,m); top:=1; build; writeln(network); end.
DINIC网络流+当前弧优化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。