iliya785

Edmonds-Karp

Jun 10th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.13 KB | None | 0 0
  1. const
  2.   sz = 11;
  3.  
  4. var
  5.   c,f:array[0..sz,0..sz] of longint;
  6.   q,cf,p:array[0..sz] of longint;
  7.   i,j,n,m,s,e,u,v,w,st,fn,maxflow:longint;
  8.  
  9. function min(a,b:longint):longint;
  10. begin
  11.   if a < b then
  12.     min:=a
  13.   else min:=b;
  14. end;
  15.  
  16. begin
  17.   read(n,m);
  18.   for i:=1 to m do
  19.     begin
  20.       read(u,v,w);
  21.       c[u][v]:=w;
  22.     end;
  23.   read(st,fn);
  24.   while True do
  25.     begin
  26.       fillchar(p,sizeof(p),0);
  27.       s:=0; e:=0; q[0]:=st;
  28.       cf[st]:=maxlongint;
  29.       while (s <= e) do
  30.         begin
  31.           v:=q[s];
  32.           for i:=1 to n do
  33.             if (c[v][i] - f[v][i] > 0) and (p[fn] = 0) and (c[v][i] <> 0) then
  34.               begin
  35.                 inc(e);
  36.                 q[e]:=i;
  37.                 p[i]:=v;
  38.                 cf[i]:=min(cf[v],c[v][i] - f[v][i]);
  39.               end;
  40.           inc(s);
  41.         end;
  42.       if p[fn] = 0 then
  43.         break;
  44.       u:=fn;
  45.       while u <> st do
  46.         begin
  47.           inc(f[p[u]][u],cf[n]);
  48.           dec(f[u][p[u]],cf[n]);
  49.           u:=p[u];
  50.         end;
  51.     end;
  52.   for i:=1 to n do
  53.     if c[st][i] <> 0 then
  54.       inc(maxflow,f[st][i]);
  55.   writeln(maxflow);
  56. end.
Advertisement
Add Comment
Please, Sign In to add comment