yaoxi-std 的博客

$\text{开}\mathop{\text{卷}}\limits^{ju\check{a}n}\text{有益}$

0%

P1251 餐巾计划问题

P1251 餐巾计划问题

题面

题目链接

解法

题目中已经暗示了要用费用流。

使用流模型,流表示餐巾的数量。但是不易处理餐巾干净与否,所以拆点,将一天拆成两个点,分别表示这天拥有的干净餐巾数量和脏的餐巾数量。为方便表述,分别设为$used{i,0}$和$used{i,1}$。

由于每天需要使用$ri$块餐巾,所以让$used{i,0}$连向汇点$T$,费用为$0$,流量为$ri$,表示这天至少需要拥有$r_i$块干净餐巾。同时让源点$S$连向$used{i,1}$,费用为$0$,流量为$r_i$,表示这天过后会多出$r_i$块脏的餐巾。

接下来考虑购买餐巾,显然可以直接从$S$连向$used{i,0}$,费用为$p$,流量为$+\infty$。而使用快洗部则从$used{i,1}$连向$used{i+m,0}$,费用为$f$,流量为$+\infty$。同理,慢洗部$used{i,1}$连向$used{i+n,0}$,费用为$s$,流量为$+\infty$。另一种保留餐巾则$used{i,1}$连向$used_{i+1,1}$,费用为$0$,流量为$+\infty$。

形式化地,定义函数addedge(u, v, cost, flow),则需要

  1. addedge(used[i][0], T, 0, r[i])
  2. addedge(S, used[i][1], 0, r[i])
  3. addedge(S, used[i][0], p, INF)
  4. if (i + 1 <= N) addedge(used[i][1], used[i + 1][1], 0, INF)
  5. if (i + m <= N) addedge(used[i][1], used[i + m][0], f, INF)
  6. if (i + n <= N) addedge(used[i][1], used[i + n][0], s, INF)

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* @file: P1251.cpp
* @author: yaoxi-std
* @url: https://www.luogu.com.cn/problem/P1251
*/
// #pragma GCC optimize ("O2")
// #pragma GCC optimize ("Ofast", "inline", "-ffast-math")
// #pragma GCC target ("avx,sse2,sse3,sse4,mmx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define resetIO(x) \
freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout)
#define debug(fmt, ...) \
fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__)
template <class _Tp>
inline _Tp& read(_Tp &x) {
bool sign = false;
char ch = getchar();
long double tmp = 1;
for (; !isdigit(ch); ch = getchar())
sign |= (ch == '-');
for (x = 0; isdigit(ch); ch = getchar())
x = x * 10 + (ch ^ 48);
if (ch == '.')
for (ch = getchar(); isdigit(ch); ch = getchar())
tmp /= 10.0, x += tmp * (ch ^ 48);
return sign ? (x = -x) : x;
}
template <class _Tp>
inline void write(_Tp x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar((x % 10) ^ 48);
}
const int MAXN = 1e4 + 10;
const int MAXM = 5e4 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;
struct Dinic {
struct Edge {
int v, cost, flow;
} edge[MAXM];
int tot = 1, cost = 0, flow = 0;
int head[MAXN], nxt[MAXM], dis[MAXN], cur[MAXN];
bool vis[MAXN];
void addedge(int u, int v, int cost, int flow) {
edge[++tot] = {v, cost, flow};
nxt[tot] = head[u], head[u] = tot;
edge[++tot] = {u, -cost, 0};
nxt[tot] = head[v], head[v] = tot;
}
bool spfa(int s, int t) {
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
queue<int> que;
que.push(s);
dis[s] = 0, vis[s] = true;
while (!que.empty()) {
int u = que.front();
que.pop(), vis[u] = false;
for (int i = head[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (edge[i].flow && dis[v] > dis[u] + edge[i].cost) {
dis[v] = dis[u] + edge[i].cost;
if (!vis[v]) {
que.push(v);
vis[v] = true;
}
}
}
}
return dis[t] != INF;
}
int augment(int u, int t, int mx) {
if (u == t || mx == 0)
return mx;
vis[u] = true;
int ret = 0;
for (int &i = cur[u]; i; i = nxt[i]) {
int v = edge[i].v;
if (vis[v] || dis[u] + edge[i].cost != dis[v])
continue;
int tmp = augment(v, t, min(mx, edge[i].flow));
mx -= tmp, ret += tmp;
edge[i].flow -= tmp, edge[i ^ 1].flow += tmp;
cost += edge[i].cost * tmp;
if (mx == 0)
break;
}
vis[u] = false;
return ret;
}
void mcmf(int s, int t) {
while (spfa(s, t)) {
memcpy(cur, head, sizeof(cur));
flow += augment(s, t, INF);
}
}
};
int n, qp, qm, qf, qn, qs;
int a[MAXN], used[MAXN][2];
Dinic mcmf;
signed main() {
read(n);
for (int i = 1; i <= n; ++i)
read(a[i]);
read(qp), read(qm), read(qf), read(qn), read(qs);
int s = 1, t = 2;
for (int i = 1; i <= n; ++i)
used[i][0] = i * 2 + 1, used[i][1] = i * 2 + 2;
for (int i = 1; i <= n; ++i) {
mcmf.addedge(used[i][0], t, 0, a[i]);
mcmf.addedge(s, used[i][1], 0, a[i]);
mcmf.addedge(s, used[i][0], qp, INF);
if (i + 1 <= n)
mcmf.addedge(used[i][1], used[i + 1][1], 0, INF);
if (i + qm <= n)
mcmf.addedge(used[i][1], used[i + qm][0], qf, INF);
if (i + qn <= n)
mcmf.addedge(used[i][1], used[i + qn][0], qs, INF);
}
mcmf.mcmf(s, t);
write(mcmf.cost), putchar('\n');
return 0;
}