本题是最小费用流问题,据说所有的线性规划问题都能变成网络流,部分贪心,一天有2种情况,分别是干净毛巾与脏毛巾,那么一个点2个状态显然困难,就将一天拆成2个点,一个点表示干净毛巾,一个表示脏毛巾,依旧是设源点汇点,源点向每天的脏毛巾点连capacity为产生的脏毛巾数,费用为0,每天的干净毛巾向汇点连capacity为需要的干净毛巾数,费用为0,购买毛巾就表示为,源点向每天的干净毛巾点连capacity为无穷,费用为p的边,每个脏毛巾向后一天的脏毛巾连capacity为无穷,费用为0的边,向m天后的干净毛巾点与n天后的干净毛巾点分别连相应的边,注意每条边不要超过总天数N
#include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; const int maxm = 3e4+5; const int INF = 0x3f3f3f3f; struct edge{ int u, v, cap, flow, cost, nex; } edges[maxm]; int head[maxm], cur[maxm], cnt, fa[4005], d[4005], N, need[2005]; bool inq[4005]; void init() { memset(head, -1, sizeof(head)); } void add(int u, int v, int cap, int cost) { edges[cnt] = edge{u, v, cap, 0, cost, head[u]}; head[u] = cnt++; } void addedge(int u, int v, int cap, int cost) { add(u, v, cap, cost), add(v, u, 0, -cost); } bool spfa(int s, int t, int &flow, LL &cost) { for(int i = 0; i <= (N<<1)+4; ++i) d[i] = INF; //init() memset(inq, false, sizeof(inq)); d[s] = 0, inq[s] = true; fa[s] = -1, cur[s] = INF; queue<int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for(int i = head[u]; i != -1; i = edges[i].nex) { edge& now = edges[i]; int v = now.v; if(now.cap > now.flow && d[v] > d[u] + now.cost) { d[v] = d[u] + now.cost; fa[v] = i; cur[v] = min(cur[u], now.cap - now.flow); if(!inq[v]) {q.push(v); inq[v] = true;} } } } if(d[t] == INF) return false; flow += cur[t]; cost += 1LL*d[t]*cur[t]; for(int u = t; u != s; u = edges[fa[u]].u) { edges[fa[u]].flow += cur[t]; edges[fa[u]^1].flow -= cur[t]; } return true; } int MincostMaxflow(int s, int t, LL &cost) { cost = 0; int flow = 0; while(spfa(s, t, flow, cost)); return flow; } void run_case() { init(); int p, m, f, n, cs; cin >> N; int s = 0, t = (N<<1)+3; for(int i = 1; i <= N; ++i) { cin >> need[i]; } cin >> p >> m >> f >> n >> cs; for(int i = 1; i <= N; ++i) { addedge(s, i<<1, need[i], 0), addedge(s, (i<<1)|1, INF, p); addedge((i<<1)|1, t, need[i], 0); if(i != N) addedge(i<<1, ((i+1)<<1), INF, 0); if(i + m <= N) addedge(i<<1, ((i+m)<<1)|1, INF, f); if(i + n <= N) addedge(i<<1, ((i+n)<<1)|1, INF, cs); } LL cost = 0; MincostMaxflow(s, t, cost); cout << cost; } int main() { ios::sync_with_stdio(false), cin.tie(0); run_case(); //cout.flush(); return 0; }