bzoj2424

时间:2021-09-06 01:31:21

比较简单的费用流,一目了然

 const inf=;
type node=record
next,point,flow,cost:longint;
end; var edge:array[..] of node;
q:array[..] of longint;
p,d,pre,cur,u,w:array[..] of longint;
v:array[..] of boolean;
s,i,len,n,m,t:longint; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure add(x,y,f,c:longint);
begin
inc(len);
edge[len].point:=y;
edge[len].flow:=f;
edge[len].cost:=c;
edge[len].next:=p[x];
p[x]:=len;
end; function spfa:boolean;
var y,i,f,r,x:longint;
begin
d[]:=;
for i:= to t do
d[i]:=inf;
f:=;
r:=;
q[]:=;
fillchar(v,sizeof(v),false);
v[]:=true;
while f<=r do
begin
x:=q[f];
v[x]:=false;
i:=p[x];
while i<>- do
begin
y:=edge[i].point;
if edge[i].flow> then
if d[y]>d[x]+edge[i].cost then
begin
d[y]:=d[x]+edge[i].cost;
pre[y]:=x;
cur[y]:=i;
if not v[y] then
begin
inc(r);
q[r]:=y;
v[y]:=true;
end;
end;
i:=edge[i].next;
end;
inc(f);
end;
if d[t]=inf then exit(false) else exit(true);
end; function mincost:longint;
var neck,i,j:longint;
begin
mincost:=;
while spfa do
begin
neck:=inf;
i:=t;
while i<> do
begin
j:=cur[i];
neck:=min(neck,edge[j].flow);
i:=pre[i];
end;
i:=t;
while i<> do
begin
j:=cur[i];
dec(edge[j].flow,neck);
inc(edge[j xor ].flow,neck);
i:=pre[i];
end;
mincost:=mincost+d[t]*neck;
end;
end; begin
len:=-;
fillchar(p,sizeof(p),);
readln(n,m,s);
t:=n+;
for i:= to n do
read(u[i]);
for i:= to n do
begin
read(w[i]);
add(,i,inf,w[i]);
add(i,,,-w[i]);
add(i,t,u[i],);
add(t,i,,);
if i<>n then
begin
add(i,i+,s,m);
add(i+,i,,-m);
end;
end;
writeln(mincost);
end.