以前写过,现在的码风与以前有些变化,主要是用数组模拟邻接表存图,以前是用指针存图。
以前的博文:http://www.cnblogs.com/Currier/p/6387732.html
洛谷可评测。
传送门:https://www.luogu.org/problem/show?pid=3381
program rrr(input,output);
const
inf=;
type
etype=record
t,c,w,next,rev:longint;
end;
var
e:array[..]of etype;
a,fre,frv,q,dis:array[..]of longint;
inq:array[..]of boolean;
n,m,s,t,i,j,x,y,c,w,max,ans,cnt,f:longint;
procedure add(x,y,c,w:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].c:=c;e[cnt].w:=w;e[cnt].next:=a[x];a[x]:=cnt;
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure spfa;
var
h,t:longint;
begin
for i:= to n do dis[i]:=inf;
fillchar(inq,sizeof(inq),false);
h:=;t:=;q[]:=s;dis[s]:=;inq[s]:=true;
while h<>t do
begin
inc(h);if h> then h:=;
i:=a[q[h]];
while i<> do
begin
if (e[i].c>) and (dis[q[h]]+e[i].w<dis[e[i].t]) then
begin
dis[e[i].t]:=dis[q[h]]+e[i].w;
frv[e[i].t]:=q[h];fre[e[i].t]:=i;
if not inq[e[i].t] then
begin
inc(t);if t> then t:=;
q[t]:=e[i].t;inq[e[i].t]:=true;
end;
end;
i:=e[i].next;
end;
inq[q[h]]:=false;
end;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m,s,t);
fillchar(a,sizeof(a),);cnt:=;
for i:= to m do
begin
readln(x,y,c,w);
add(x,y,c,w);e[cnt].rev:=cnt+;
add(y,x,,-w);e[cnt].rev:=cnt-;
end;
max:=;ans:=;
while true do
begin
spfa;
if dis[t]=inf then break;
j:=t;f:=inf;
while j<>s do begin f:=min(f,e[fre[j]].c);j:=frv[j]; end;
max:=max+f;
j:=t;w:=;
while j<>s do begin w:=w+e[fre[j]].w;dec(e[fre[j]].c,f);inc(e[e[fre[j]].rev].c,f);j:=frv[j]; end;
ans:=ans+w*f;
end;
write(max,' ',ans);
close(input);close(output);
end.