模板题草地排水
var
f:array[1..1000,0..1000] of longint;
a,h:array[0..1000]of longint;
m,n,i,j,k,x,y,z,t,ans,b:longint;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
function dg(k,t:longint):longint;
var
i,qq,minh:longint;
begin
if k=m then exit(t);
qq:=0;
minh:=m+1;
for i:=1 to m do
begin
if f[k,i]>0 then
begin
if h[k]=h[i]+1 then
begin
qq:=dg(i,min(t,f[k,i]));
if qq>0 then
begin
f[i,k]:=f[i,k]+qq;
f[k,i]:=f[k,i]-qq;
exit(qq);
end;
if h[1]>m then exit(0);
end;
minh:=min(minh,h[i]+1);
end;
end;
dec(a[h[k]]);
if a[h[k]]=0 then h[1]:=m+1;
h[k]:=minh;
inc(a[h[k]]);
exit(0);
end;
begin
assign(input,'wangluoliu.in'); reset(input);
assign(output,'wangluoliu.out'); rewrite(output);
read(n,m);
for i:=1 to n do
begin
read(x,y,z);
f[x,y]:=f[x,y]+z;
end;
ans:=0;
a[0]:=m;
while h[1]<=m do ans:=ans+dg(1,maxlongint);
writeln(ans);
close(input); close(output);
end.
模板题:【NOI2006】最大获利
var
f,next,head,up,data,bz,d:array[0..400000] of longint;
m,mm,n,i,j,k,x,y,z,t,ans,bb,ak:longint;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
function bfs:boolean;
var
i,j,k:longint;
begin
fillchar(bz,sizeof(bz),0);
d[1]:=1;
bz[1]:=1;
i:=0;
j:=1;
while i<j do
begin
inc(i);
k:=head[d[i]];
while k<>0 do
begin
if (bz[f[k]]=0)and(data[k]>0) then
begin
inc(j);
d[j]:=f[k];
bz[f[k]]:=bz[d[i]]+1;
end;
k:=next[k];
end;
end;
if bz[m]<>0 then exit(true) else exit(false);
end;
function dg(k,t:longint):longint;
var
i,qq,ans:longint;
begin
if k=m then exit(t);
qq:=0;
ans:=0;
i:=head[k];
while i<>0 do
begin
if (data[i]>0)and(bz[f[i]]=bz[k]+1) then
begin
qq:=dg(f[i],min(t,data[i]));
if qq>0 then
begin
inc(data[up[i]],qq);
dec(data[i],qq);
inc(ans,qq);
t:=t-qq;
if t=0 then break;
end;
end;
i:=next[i];
end;
exit(ans);
end;
procedure put(x,y,z:longint);
begin
inc(bb);
data[bb]:=z;
f[bb]:=y;
next[bb]:=head[x];
head[x]:=bb;
up[bb]:=bb+1;
inc(bb);
data[bb]:=0;
f[bb]:=x;
next[bb]:=head[y];
head[y]:=bb;
up[bb]:=bb-1;
end;
begin
read(n,mm);
m:=mm+n+2;
bb:=0;
fillchar(head,sizeof(head),0);
for i:=1 to n do
begin
read(x);
put(i+mm+1,m,x);
end;
ak:=0;
for i:=1 to mm do
begin
read(x,y,z);
put(1+i,mm+x+1,maxlongint);
put(1+i,mm+y+1,maxlongint);
put(1,1+i,z);
ak:=ak+z;
end;
ans:=0;
while bfs do ans:=ans+dg(1,maxlongint);
writeln(ak-ans);
end.
dinic好像快点