Cod sursa(job #1403060)

Utilizator casianos1996Marc Casian Nicolae casianos1996 Data 26 martie 2015 23:39:34
Problema Flux maxim Scor 30
Compilator fpc Status done
Runda Arhiva educationala Marime 1.43 kb
program flux_brut;
var     f,g:text;
        maxf,min,pas,k,p,n,m,i,j,c,x,y,s,d:longint;
        a:array[1..1000,1..1000] of longint;
        viz,cd,pred:array[1..1000] of integer;

procedure bfs(nod,mark:integer; var min:longint);
var       p,i,st,sf:longint;
begin
  min:=maxlongint;
  st:=0; sf:=1; cd[1]:=nod; pred[nod]:=0; viz[nod]:=mark;
  while st<sf do
    begin
      inc(st);
      for i:=1 to n do
        begin
          if a[cd[st],i]>0 then
            if viz[i]<>mark then
              begin
                if min>a[cd[st],i] then min:=a[cd[st],i];
                inc(sf);
                cd[sf]:=i;
                viz[cd[sf]]:=mark;
                pred[cd[sf]]:=cd[st];
              end;
        end;
    end;
end;

function pot(sursa,destinatie,mark:longint; var min:longint):boolean;
begin
  bfs(1,mark,min);
  if (viz[sursa]=mark) and (viz[destinatie]=mark) then pot:=true
    else pot:=false;
end;

begin
  assign(f,'maxflow.in'); reset(f);
  assign(g,'maxflow.out'); rewrite(g);
  readln(f,n,m);
  maxf:=0;
  for i:=1 to m do
    begin
      readln(f,x,y,c);
      a[x,y]:=c;
    end;
  pas:=1;
  while pot(1,n,pas,min) do
    begin
      maxf:=maxf+min;
      i:=pred[n];
      j:=n;
      while i<>0 do
        begin
          a[i,j]:=a[i,j]-min;
          j:=i;
          i:=pred[j];
        end;
      inc(pas);
    end;
  writeln(g,maxf);
  close(f); close(g);
end.