2424: [HAOI2010]订货
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=2424
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
Sample Output
34
HINT
题意
题解:
费用流,S--月--T
|
下个月
代码:
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> using namespace std; const int MAXN = 10000; const int MAXM = 100000; const int INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow, cost; int x, y; } edge[MAXM],HH[MAXN],MM[MAXN]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N, M; char map[MAXN][MAXN]; void init() { N = MAXN; tol = 0; memset(head, -1, sizeof(head)); } void addedge(int u, int v, int cap, int cost)//左端点,右端点,容量,花费 { edge[tol]. to = v; edge[tol]. cap = cap; edge[tol]. cost = cost; edge[tol]. flow = 0; edge[tol]. next = head[u]; head[u] = tol++; edge[tol]. to = u; edge[tol]. cap = 0; edge[tol]. cost = -cost; edge[tol]. flow = 0; edge[tol]. next = head[v]; head[v] = tol++; } bool spfa(int s, int t) { queue<int>q; for(int i = 0; i < N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = edge[i]. next) { int v = edge[i]. to; if(edge[i]. cap > edge[i]. flow && dis[v] > dis[u] + edge[i]. cost ) { dis[v] = dis[u] + edge[i]. cost; pre[v] = i; if(!vis[v]) { vis[v] = true; q.push(v); } } } } if(pre[t] == -1) return false; else return true; } //返回的是最大流, cost存的是最小费用 int minCostMaxflow(int s, int t, int &cost) { int flow = 0; cost = 0; while(spfa(s,t)) { int Min = INF; for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]) { if(Min > edge[i]. cap - edge[i]. flow) Min = edge[i]. cap - edge[i]. flow; } for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]) { edge[i]. flow += Min; edge[i^1]. flow -= Min; cost += edge[i]. cost * Min; } flow += Min; } return flow; } int main() { int n, m, ss; scanf("%d%d%d",&n,&m,&ss); int ch = 0, cm = 0; init();//注意 int beg = 0;//超级起点 int end = 3000;//超级汇点 for(int i=1;i<=n;i++) { int x;scanf("%d",&x); addedge(i,end,x,0); } for(int i=1;i<=n;i++) { int x;scanf("%d",&x); addedge(0,i,9999999,x); } for(int i=1;i<n;i++)addedge(i,i+1,ss,m); int ans = 0; minCostMaxflow(beg,end,ans); printf("%d\n",ans); }
时间: 2024-10-27 07:05:42