Pagini recente » Cod sursa (job #1171073) | Cod sursa (job #228653) | Monitorul de evaluare | Cod sursa (job #1947473) | Cod sursa (job #775231)
Cod sursa(job #775231)
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;
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;
{ for i:=1 to lev do 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; }
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;
{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; }
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;
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.