BZOJ2424: [HAOI2010]订货
2424: [HAOI2010]订货
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 420 Solved: 275
[Submit][Status]
Description
某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为零,问如何安排这n个月订购计划,才能使成本最低?每月月初订购,订购后产品立即到货,进库并供应市场,于当月被售掉则不必付存贮费。假设仓库容量为S。
Input
第1行:n, m, S (0<=n<=50, 0<=m<=10, 0<=S<=10000)
第2行:U1 , U2 , ... , Ui , ... , Un (0<=Ui<=10000)
第3行:d1 , d2 , ..., di , ... , dn (0<=di<=100)
Output
只有1行,一个整数,代表最低成本
Sample Input
3 1 1000
2 4 8
1 2 4
2 4 8
1 2 4
Sample Output
34
HINT
Source
题解:
这题算是比较裸的费用流了
需要注意的一点就是i 向 i+n 连边的容量应该是仓库容量+该天的需求量
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 const inf=maxlongint; 2 type node=record 3 from,go,next,v,c:longint; 4 end; 5 var e:array[0..2000000] of node; 6 pre,head,q,d:array[0..1000000] of longint; 7 v:array[0..1000000] of boolean; 8 i,j,n,m,maxf,s,t,l,r,mincost,tot,x:longint; 9 function min(x,y:longint):longint; 10 begin 11 if x<y then exit(x) else exit(y); 12 end; 13 procedure ins(x,y,z,w:longint); 14 begin 15 inc(tot); 16 with e[tot] do 17 begin 18 from:=x;go:=y;v:=z;c:=w;next:=head[x];head[x]:=tot; 19 end; 20 end; 21 procedure insert(x,y,z,w:longint); 22 begin 23 ins(x,y,z,w);ins(y,x,0,-w); 24 end; 25 function spfa:boolean; 26 var i,x,y:longint; 27 begin 28 fillchar(v,sizeof(v),false); 29 for i:=s to t do d[i]:=inf; 30 l:=0;r:=1;q[1]:=s;d[s]:=0;v[s]:=true; 31 while l<r do 32 begin 33 inc(l); 34 x:=q[l];v[x]:=false; 35 i:=head[x]; 36 while i<>0 do 37 begin 38 y:=e[i].go; 39 if (e[i].v<>0) and (d[x]+e[i].c<d[y]) then 40 begin 41 d[y]:=d[x]+e[i].c; 42 pre[y]:=i; 43 if not(v[y]) then 44 begin 45 v[y]:=true; 46 inc(r); 47 q[r]:=y; 48 end; 49 end; 50 i:=e[i].next; 51 end; 52 end; 53 exit(d[t]<>inf); 54 end; 55 procedure mcf; 56 var i,tmp:longint; 57 begin 58 mincost:=0; 59 while spfa do 60 begin 61 tmp:=inf; 62 i:=pre[t]; 63 while i<>0 do 64 begin 65 tmp:=min(tmp,e[i].v); 66 i:=pre[e[i].from]; 67 end; 68 inc(mincost,tmp*d[t]); 69 i:=pre[t]; 70 while i<>0 do 71 begin 72 dec(e[i].v,tmp); 73 inc(e[i xor 1].v,tmp); 74 i:=pre[e[i].from]; 75 end; 76 end; 77 end; 78 procedure init; 79 begin 80 tot:=1; 81 readln(n,m,maxf); 82 s:=0;t:=2*n+1; 83 for i:=1 to n do 84 begin 85 read(x); 86 insert(i+n,t,x,0); 87 insert(i,i+n,maxf+x,0); 88 end; 89 for i:=1 to n do 90 begin 91 read(x); 92 insert(s,i,inf,x); 93 end; 94 for i:=1 to n-1 do insert(i+n,i+1,inf,m); 95 end; 96 procedure main; 97 begin 98 mincost:=0; 99 mcf; 100 writeln(mincost); 101 end; 102 begin 103 assign(input,'input.txt');assign(output,'output.txt'); 104 reset(input);rewrite(output); 105 init; 106 main; 107 close(input);close(output); 108 end.