3.30:

这篇是以前写的,用的还是指针存图,今天又写了个代码,码风稍微好看点。

传送门:http://www.cnblogs.com/Currier/p/6648685.html

---------------------------------------------------一点也不华丽的分割线---------------------------------------------------------

最小费用最大流(洛谷可评测):

 program rrr(input,output);
const
inf=;
type
pointer=^nodetype;
nodetype=record
t,c,w:longint;
next,rev:pointer;
end;
var
a,fre:array[..]of pointer;
p:pointer;
dis,q,frv:array[..]of longint;
v:array[..]of boolean;
n,m,s,t,x,y,c,w,i,sum,f,ans,max:longint;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure ins(x,y,c,w:longint);
begin
new(p);p^.t:=y;p^.c:=c;p^.w:=w;p^.next:=a[x];a[x]:=p;
end;
procedure add(x,y,c,w:longint);
begin
ins(x,y,c,w);ins(y,x,,-w);
a[x]^.rev:=a[y];a[y]^.rev:=a[x];
end;
procedure spfa;
var
h,t:longint;
begin
for i:= to n do dis[i]:=inf;
fillchar(v,sizeof(v),false);
h:=;t:=;q[]:=s;dis[s]:=;v[s]:=true;
while h<>t do
begin
inc(h);if h> then h:=;
p:=a[q[h]];
while p<>nil do
begin
if (p^.c>) and (dis[q[h]]+p^.w<dis[p^.t]) then
begin
dis[p^.t]:=dis[q[h]]+p^.w;
frv[p^.t]:=q[h];fre[p^.t]:=p;
if not v[p^.t] then
begin
inc(t);if t> then t:=;
q[t]:=p^.t;v[p^.t]:=true;
end;
end;
p:=p^.next;
end;
v[q[h]]:=false;
end;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m,s,t);
for i:= to m do begin read(x,y,c,w);add(x,y,c,w); end;
ans:=;max:=;
while true do
begin
spfa;
if dis[t]=inf then break;
i:=t;f:=inf;
while i<>s do begin f:=min(f,fre[i]^.c);i:=frv[i]; end;
max:=max+f;
i:=t;sum:=;
while i<>s do begin fre[i]^.c:=fre[i]^.c-f;fre[i]^.rev^.c:=fre[i]^.rev^.c+f;sum:=sum+fre[i]^.w;i:=frv[i]; end;
ans:=ans+sum*f;
end;
write(max,' ',ans);
close(input);close(output);
end.
05-11 03:41