在连续的N天里面,餐厅每天需要的餐巾数为ri,新买一块餐巾需要p元,送到快洗部,每块f元,需m天洗好,送到慢洗部,每块s元,要n天洗好,每天可以选择送或者不送。求N天的最小费用。
http://wikioi.com/problem/1237/
将每天拆成两个点,要用的Y和用过的X。
显然要从源点向X连一条容量为ri费用为0的边,从Y向汇点连一条容量为ri费用为0的边。
餐巾可以买新的,所以从源点向Y(要用的)连一条容量为inf费用为p的边;
可以送到快洗部,从X向Y+m(Y+m<=N)连一条容量为inf费用为f的边;
可以送到慢洗部,从X向Y+n(Y+n<=N)连一条容量为inf费用为s的边;
还可以留到下一天送洗,从X向X+1(X+1<=N)连一条容量为inf费用为0的边。
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double DB;
typedef long long ll;
typedef pair<int, int> PII;
#define pb push_back
#define MP make_pair
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const DB eps = 1e-6;
const int inf = ~0U>>1;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
const int maxn = 40000 + 10;
///先调用init,然后while(spfa()) dfs();
const int maxv = 10000 + 10;///顶点数
const int maxe = 1000000 + 10;///边数
struct node{
int u, v, w, cap, next; ///cap = 容量, w = 费用
}edge[maxe];
int head[maxv], d[maxv], pre[maxv];
bool vis[maxv];
int cnt, st, ed, maxflow, mincost;
void addedge(int v, int u, int cap, int w){
edge[cnt].u = v; edge[cnt].v = u; edge[cnt].w = w; edge[cnt].cap = cap; edge[cnt].next = head[v]; head[v] = cnt++;
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = -w; edge[cnt].cap = 0; edge[cnt].next = head[u]; head[u] = cnt++;
//printf("u:%d, v:%d, cap:%d, w:%d\n", u, v, cap, w);
}
bool spfa(){///找最短路
memset(vis, false, sizeof(vis));
memset(d, 0x3f, sizeof(d));
int maxt = d[0];
d[st] = 0; pre[st] = -1; vis[st] = 1;
queue<int> Q;
Q.push(st);
while(!Q.empty()){
int now = Q.front(); Q.pop();
vis[now] = 0;
for(int i=head[now]; ~i; i=edge[i].next){
int k = edge[i].v;
if(edge[i].cap && d[now] + edge[i].w < d[k]){
d[k] = d[now] + edge[i].w;
pre[k] = i; ///通过pre数组查找增广路径上的边,求出残留容量的最小值
if(!vis[k]){
vis[k] = 1; Q.push(k);
}
}
}
}
return d[ed] < maxt;
}
void dfs(){///流过的值是最短路中容量最小的
int mi = 0x3f3f3f3f;
for(int i=pre[ed]; ~i; i=pre[edge[i].u])mi = min(mi, edge[i].cap);
maxflow += mi;
for(int i=pre[ed]; ~i; i=pre[edge[i].u]) {
edge[i].cap -= mi; edge[i ^ 1].cap += mi;
mincost += mi * edge[i].w;
}
}
void init(int source, int sink){
memset(head, -1, sizeof(head)); cnt = 0;
st = source; ed = sink;///源点和汇点
maxflow = mincost = 0;///最大流和最小费用
}
int N, p, m, f, n, s, r;
int main(){
scanf("%d%d%d%d%d%d", &N, &p, &m, &f, &n, &s);
init(0, 2 * N + 1);
for(int i=1; i<=N; i++){
scanf("%d", &r);
addedge(st, i + N, inf, p);
addedge(i + N, ed, r, 0);
addedge(st, i, r, 0);
if(i != N) addedge(i, i + 1, inf, 0);
if(i + m <= N) addedge(i, i + m + N, inf, f);
if(i + n <= N) addedge(i, i + n + N, inf, s);
}
while(spfa()) dfs();
printf("%d\n", mincost);
return 0;
}