Pagini recente » Cod sursa (job #2637059) | Cod sursa (job #2774018) | Cod sursa (job #423354) | Cod sursa (job #2870356) | Cod sursa (job #775224)
Cod sursa(job #775224)
Program maxflow;
type lista=^celula;
celula=record
nod:longint;
next:lista;
end;
tip=record x,y:longint; end;
var graf:array [1..1000] of lista;
v:lista;
cost,direct: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;
for i:=1 to lev do
if direct[st[i].x,st[i].y]<>0 then begin
cost[st[i].x,st[i].y]:=cost[st[i].x,st[i].y]-val;
cost[st[i].y,st[i].x]:=cost[st[i].y,st[i].x]+val;
end
else cost[st[i].x,st[i].y]:=cost[st[i].x,st[i].y]-val;
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);
dec(lev);
end;
p:=p^.next;
end;
end;
end;
begin
assign(fi,'maxflow.in');
assign(fo,'maxflow.out');
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; direct[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.