[HAOI2010][BZOJ2424] 订货
2424: [HAOI2010]订货
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 639 Solved: 426
[Submit][Status][Discuss]
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
费用流裸题……5分钟敲完过了。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<set> #include<map> #include<vector> #include<queue> #define MOD 100 #define MAXN 101 #define MAXM 1000 #define INF 10000007 using namespace std; int next[MAXM],from[MAXM],list[MAXM],flow[MAXM],cost[MAXM]; int head[MAXN],dis[MAXN],q[MAXN],path[MAXN]; bool v[MAXN]; int cnt=1,n,m,s,x,ans=0; void insert(int x,int y,int w,int z) { next[++cnt]=head[x]; head[x]=cnt; from[cnt]=x; list[cnt]=y; flow[cnt]=w; cost[cnt]=z; } bool spfa() { memset(v,0,sizeof(v)); for (int i=0;i<=n+1;i++) dis[i]=INF; dis[0]=1; q[1]=0; v[0]=1; int t=0,w=1,x; while (t!=w) { t=(t+1)%MOD; x=q[t]; for (int i=head[x];i;i=next[i]) if (flow[i]&&dis[list[i]]>cost[i]+dis[x]) { dis[list[i]]=dis[x]+cost[i]; path[list[i]]=i; if (!v[list[i]]) { v[list[i]]=1; w=(w+1)%MOD; q[w]=list[i]; } } v[x]=0; } return dis[n+1]!=INF; } void mcf() { int x=INF,i; i=path[n+1]; while (i) { x=min(x,flow[i]); i=path[from[i]]; } i=path[n+1]; while (i) { ans+=x*cost[i]; flow[i]-=x; flow[i^1]+=x; i=path[from[i]]; } } int main() { scanf("%d%d%d",&n,&m,&s); for (int i=1;i<=n;i++) { scanf("%d",&x); insert(i,n+1,x,0); insert(n+1,i,0,0); } for (int i=1;i<=n;i++) { scanf("%d",&x); insert(0,i,INF,x); insert(i,0,0,-x); } for (int i=1;i<n;i++) { insert(i,i+1,s,m); insert(i+1,i,0,-m); } while (spfa()) mcf(); printf("%d",ans); return 0; }