为防止广告,目前nocow只有登录用户能够创建新页面。如要创建页面请先登录/注册(新用户需要等待1个小时才能正常使用该功能)。

Edmonds-Karp pascal

来自NOCOW
跳转到: 导航, 搜索
//written by PzFirzen
{
PROG:ditch
LANG:PASCAL
}
program ditch;
 var t,g:array[0..500,0..500]of longint;
     tot,d,min,r:array[0..500]of longint;
     v:array[0..500]of boolean;
     q:array[0..500]of longint;
     open,closed,i,x,y,m,n,s,e,c,max,j:longint;
procedure sssp;
begin
 fillchar(v,sizeof(v),false);v[1]:=true;
 fillchar(d,sizeof(d),0);
 fillchar(min,sizeof(min),$7f);
 q[1]:=1;closed:=1;open:=0;
 repeat
  inc(open);
  x:=q[open];
  for i:=1 to tot[x] do
   begin
    y:=t[x,i];
    if (g[x,y]>0)and(not v[y]) then
    begin
     inc(closed);
     q[closed]:=y;
     v[y]:=true;
     d[y]:=d[x]+1;
     r[y]:=x;
     if g[x,y]<min[x] then min[y]:=g[x,y] else min[y]:=min[x];
     if y=m then exit;
    end;
   end;
 until open>=closed;
end;
begin
 assign(input,'ditch.in');
 assign(output,'ditch.out');
 reset(input);
 readln(n,m);
 for i:=1 to n do
 begin
  readln(s,e,c);
  inc(tot[s]);t[s,tot[s]]:=e;
  inc(tot[e]);t[e,tot[e]]:=s;
  inc(g[s,e],c);
 end;
 close(input);  rewrite(output);
 while true do
 begin
 sssp;
 if d[m]=0 then break else
 begin
  y:=m;
  for i:=1 to d[m] do
   begin
    x:=r[y];
    g[x,y]:=g[x,y]-min[m];
    g[y,x]:=g[y,x]+min[m];
    y:=x;
   end;
  inc(max,min[m]);
 end;
 end;
 writeln(max);
 close(output);
end.
个人工具