简单基础网络流,用sap算法,不过这道题有多重边,被阴了,WA一次,还有就是这题有多组数据(居然没看到...)
program poj1273;
var
n,m,i,j,i1,j1:integer;
ans,min,p:longint;
flag:boolean;
g:array[1..200,1..200] of longint;
fir,now,t,num,liu:array[0..200] of longint;(fir是表示增广路i的前一个节点,now是当前边的两个端点为i,now,t是顶点数为i的节点的个数,num是第i个节点的编号,liu是当前节点1到i的流量
procedure init;
var
x,y,z:longint;
begin
readln(m,n);
for i:=1 to m do
begin
readln(x,y,z);
inc(g[x,y],z);
end;
end;
begin
while not eof do
begin
fillchar(g,sizeof(g),0);
fillchar(fir,sizeof(fir),0);
fillchar(now,sizeof(now),0);
fillchar(t,sizeof(t),0);
fillchar(num,sizeof(num),0);
fillchar(liu,sizeof(liu),0);
init;
t[0]:=n;
for i:=1 to n do
now:=1;
ans:=0;
i:=1;
p:=maxlongint;
while num<n do
begin
flag:=false;
liu:=p;
for j:=now to n do
if (g[i,j]>0) and (num[j]+1=num) then
begin
flag:=true;
if g[i,j]<p then p:=g[i,j];
fir[j]:=i;
now:=j;
i:=j;
if i=n then
begin
inc(ans,p);
while i<>1 do
begin
i1:=i;
i:=fir;
dec(g[i,i1],p);
inc(g[i1,i],p);
end;
p:=maxlongint;
end;
break;
end;
if flag then continue;
min:=n-1;//没有允许弧了,需要重标号
for j:=1 to n do
if (g[i,j]>0) and (num[j]<min) then
begin
j1:=j;
min:=num[j];
end;
now:=j1;
dec(t[num]);//GAP优化
if t[num]=0 then break;
num:=min+1;
inc(t[num]);
if i<>1 then
begin
i:=fir;
p:=liu;
end;
end;
writeln(ans);
end;
end.