Cod sursa(job #300362)

Utilizator cheery_g1rlHaller Emanuela cheery_g1rl Data 7 aprilie 2009 13:24:27
Problema Flux maxim de cost minim Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.86 kb
var a:array[1..510,0..510] of longint;
    c,f,co:array[1..510,1..510] of longint;
    tata,d:array[1..510] of longint;
    s,dd,n,m,i,x,y,z,t,fmin,maxx:longint;
    flux:int64;
function bfs:boolean; inline;
    var viz,q:array[1..5100] of longint;
        p,u:longint;
    begin
      fillchar(d,sizeof(d),maxx);
      fillchar(tata,sizeof(tata),0);
      fillchar(viz,sizeof(viz),0);
      d[s]:=0; tata[s]:=s;viz[s]:=1;q[1]:=s; p:=1; u:=1;
      while p<=u do
         begin
           x:=q[p];
           for i:=1 to a[x,0] do
              begin
                y:=a[x,i];
                if (d[y]>d[x]+co[x,y])and(c[x,y]>f[x,y]) then
                   begin
                     d[y]:=d[x]+co[x,y];
                     tata[y]:=x;
                     if viz[y]=0 then
                       begin
                         viz[y]:=1;
                         inc(u); q[u]:=y;
                       end;
                   end;
              end;
           viz[x]:=0;
           inc(p);
         end;
       if d[dd]<>maxx then bfs:=true
          else bfs:=false;
    end;
begin
assign(input,'fmcm.in'); reset(input);
assign(output,'fmcm.out'); rewrite(output);
readln(n,m,s,dd);
maxx:=20000000;
for i:=1 to m do
   begin
     readln(x,y,z,t);
     c[x,y]:=z;
     co[x,y]:=t;        co[y,x]:=-t;
     inc(a[x,0]);       a[x,a[x,0]]:=y;
     inc(a[y,0]);       a[y,a[y,0]]:=x;
   end;
while bfs do
   begin
     x:=dd;
     fmin:=maxx;
     while x<>s do
       begin
         if fmin>c[tata[x],x]-f[tata[x],x] then fmin:=c[tata[x],x]-f[tata[x],x];
         x:=tata[x];
       end;
     x:=dd;
     while x<>s do
       begin
         f[tata[x],x]:=f[tata[x],x]+fmin;
         f[x,tata[x]]:=f[x,tata[x]]-fmin;
         x:=tata[x];
       end;
     flux:=flux+fmin*d[dd];
   end;
writeln(flux);
close(output);
close(input);
end.