1 条题解
-
0
C++ :
#include <cstdio> #include <climits> #include <algorithm> #include <queue> const int MAXN = 1000; const int MAXR = 50; struct Node; struct Edge; struct Node { Edge *firstEdge, *inEdge; int flow, dist; bool inQueue; } nodes[MAXN * 2 + 2]; struct Edge { Node *from, *to; int flow, capacity, cost; Edge *next, *reversedEdge; Edge(Node *from, Node *to, int capacity, int cost) : from(from), to(to), capacity(capacity), flow(0), cost(cost), next(from->firstEdge) {} }; int n, a[MAXN], P, M, N, F, S; struct EdmondsKarp { bool bellmanford(Node *s, Node *t, int n, int &flow, int &cost) { for (int i = 0; i < n; i++) { nodes[i].dist = INT_MAX; nodes[i].inEdge = NULL; nodes[i].flow = 0; nodes[i].inQueue = false; } std::queue<Node *> q; q.push(s); s->dist = 0; s->flow = INT_MAX; while (!q.empty()) { Node *v = q.front(); q.pop(); v->inQueue = false; for (Edge *e = v->firstEdge; e; e = e->next) { if (e->flow < e->capacity && e->to->dist > v->dist + e->cost) { e->to->dist = v->dist + e->cost; e->to->inEdge = e; e->to->flow = std::min(v->flow, e->capacity - e->flow); if (!e->to->inQueue) { q.push(e->to); e->to->inQueue = true; } } } } if (t->dist == INT_MAX) return false; for (Edge *e = t->inEdge; e; e = e->from->inEdge) { e->flow += t->flow; e->reversedEdge->flow -= t->flow; } flow += t->flow; cost += t->flow * t->dist; return true; } void operator()(int s, int t, int n, int &flow, int &cost) { flow = cost = 0; while (bellmanford(&nodes[s], &nodes[t], n, flow, cost)); } } edmondskarp; inline void addEdge(int from, int to, int capacity, int cost) { nodes[from].firstEdge = new Edge(&nodes[from], &nodes[to], capacity, cost); nodes[to].firstEdge = new Edge(&nodes[to], &nodes[from], 0, -cost); nodes[from].firstEdge->reversedEdge = nodes[to].firstEdge, nodes[to].firstEdge->reversedEdge = nodes[from].firstEdge; } inline int dayID(int i, int d) { return d == 0 ? i : i + n; } inline void print(Node *v) { int x = (int)(v - nodes); if (x == 0) putchar('s'); else if (x == n * 2 + 1) putchar('t'); else if (x <= n) printf("IN(%d)", x); else printf("OUT(%d)", x - n); } int main() { scanf("%d", &n); scanf("%d %d %d %d %d", &P, &M, &F, &N, &S); for (int i = 0; i < n; i++) scanf("%d", &a[i]); const int s = 0, t = n * 2 + 1; for (int i = 1; i <= n; i++) { if (i + M <= n) addEdge(dayID(i, 0), dayID(i + M, 1), INT_MAX, F); if (i + N <= n) addEdge(dayID(i, 0), dayID(i + N, 1), INT_MAX, S); if (i + 1 <= n) addEdge(dayID(i, 0), dayID(i + 1, 0), INT_MAX, 0); addEdge(s, dayID(i, 1), INT_MAX, P); addEdge(s, dayID(i, 0), a[i - 1], 0); addEdge(dayID(i, 1), t, a[i - 1], 0); } int flow, cost; edmondskarp(s, t, n * 2 + 2, flow, cost); printf("%d\n", cost); return 0; }
- 1
信息
- ID
- 996
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者