Cod sursa(job #775232)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 7 august 2012 16:34:19
Problema Flux maxim Scor 60
Compilator fpc Status done
Runda Arhiva educationala Marime 1.83 kb
Program maxflow;
 type lista=^celula;
      celula=record
        nod:integer;
        next:lista;
        end;
      tip=record x,y:integer; end;
var graf:array [1..1000] of lista;
    v:lista;
    b1:array [1..1 shl 10] of char;
    cost:array [1..1001,1..1001] of longint;
    st:array [1..1000] of tip;
    viz:array [1..1000] of boolean;
    n,m,i,flux,x,y,c,val,lev:longint;
    ok:boolean;
    fi,fo:text;
procedure dfs(nod,min:longint);
 var p:lista;
begin
 if nod=n then begin
               flux:=flux+min; val:=min; ok:=true;
                end
   else begin
         p:=graf[nod]; viz[nod]:=true;
         while p<>nil do begin
          if (viz[p^.nod]=false) and (ok=false) and (cost[nod,p^.nod]>0) then begin
                                 if cost[nod,p^.nod]<min then min:=cost[nod,p^.nod];
                                  inc(lev); st[lev].x:=nod; st[lev].y:=p^.nod;
                                 dfs(p^.nod,min);
                        if (ok=true) and (lev>0) then begin
                             cost[st[lev].x,st[lev].y]:=cost[st[lev].x,st[lev].y]-val;
                              cost[st[lev].y,st[lev].x]:=cost[st[lev].y,st[lev].x]+val;
                               end;
                           dec(lev);
                                 end;
           p:=p^.next;
         end;
        end;
end;
begin
 assign(fi,'maxflow.in');
  assign(fo,'maxflow.out');
 settextbuf(fi,b1);
 reset(fi); rewrite(fo); readln(fi,n,m);
 for i:=1 to m do begin
    readln(fi,x,y,c);
     new(v); v^.nod:=y; v^.next:=graf[x];
      graf[x]:=v; cost[x,y]:=c;
     new(v); v^.nod:=x; v^.next:=graf[y]; graf[y]:=v;
    end;
 ok:=true;
  while ok do begin
           ok:=false;
            dfs(1,100000000); fillchar(viz,sizeof(viz),0);
           end;
  write(fo,flux);
 close(fo);
end.