procedure add(u,v,c:longint);
begin
inc(e);
g[e].y:=v; g[e].c:=c; g[e].op:=e+1; g[e].next:=ls[u]; ls[u]:=e;
inc(e);
g[e].y:=u; g[e].c:=0; g[e].op:=e-1; g[e].next:=ls[v]; ls[v]:=e;
end;
function bfs:boolean;
var i,head,tail,tt,u:longint;
begin
for i:=s to t do
dis[i]:=0;
dis[s]:=1;
head:=0; tail:=1;
inc(tail);
q[tail]:=s;
while head<=tail do
begin
inc(head);
u:=q[head];
i:=ls[u];
while i>0 do
begin
if (g[i].c<>0) and (dis[g[i].y]=0) then
begin
dis[g[i].y]:=dis[u]+1;
if g[i].y=t then exit(true);
inc(tail);
q[tail]:=g[i].y;
end;
i:=g[i].next;
end;
end;
exit(false);
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x)
else exit(y);
end;
function dfs(x,maxf:longint):longint;
var ret,i,f:longint;
begin
if (x=t) or (maxf=0) then exit(maxf);
ret:=0;
i:=cur[x];
while i>0 do
begin
if (g[i].c<>0) and (dis[g[i].y]=dis[x]+1) then
begin
f:=dfs(g[i].y,min(g[i].c,maxf));
dec(g[i].c,f);
inc(g[g[i].op].c,f);
ret:=ret+f;
if ret=maxf then break;
end;
i:=g[i].next;
end;
exit(ret);
end;
procedure dinic;
var i:longint;
begin
while bfs do
begin
for i:=s to t do
cur[i]:=ls[i];
ans:=ans+dfs(s,maxlongint);
end;
end;
网络流模版
最新推荐文章于 2022-10-25 23:13:43 发布