【NOI2008】志愿者招募
Description
申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管。布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者。经过估算,这个项目需要N 天才能完成,其中第i 天至少需要Ai 个人。布布通过了解得知,一共有M 类志愿者可以招募。其中第i 类可以从第Si 天工作到第Ti 天,招募费用是每人Ci 元。新官上任三把火,为了出色地完成自己的工作,布布希望用尽量少的费用招募足够的志愿者,但这并不是他的特长!于是布布找到了你,希望你帮他设计一种最优的招募方案。
Solution
一道比较妙的费用流。
考虑建图。
对于每天至少需要
A
[
i
]
A[i]
A[i]的限制,我们从
i
i
i天向
i
+
1
i+1
i+1天连一条边,流量为
i
n
f
−
A
[
i
]
inf-A[i]
inf−A[i],费用为
0
0
0.
对于每个人从
s
[
i
]
s[i]
s[i]到
t
[
i
]
t[i]
t[i]工作,为了保证这一整个区间都工作,我们直接从
s
[
i
]
s[i]
s[i]向
t
[
i
]
t[i]
t[i]连一条边,流量为
i
n
f
inf
inf,费用为
c
[
i
]
c[i]
c[i]
然后从源点向第一天连一条边,最后一天向汇点连一条边,跑个费用流即可
Code
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int cnt, la[10000], inq[10000], cost, n, m, fl[10000], pre[10000], dist[10000], e[10000], ans, maxf;
const int inf = 1e9;
inline int read() {
int res = 0, flag = 1; char ch = getchar();
for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') flag = -1;
for(; ch >= '0' && ch <= '9'; res = res * 10 + ch - '0', ch = getchar());
return res * flag;
}
struct Edge {
int nxt, flow, to, cost;
}edge[1001000];
void build(int u, int v, int f, int cost) {
edge[cnt].flow = f;
edge[cnt].to = v;
edge[cnt].nxt = la[u];
edge[cnt].cost = cost;
la[u] = cnt;
++cnt;
}
bool SPFA() {
queue <int> q;
q.push(0);
for(int i = 1; i <= n + 1; ++i)
inq[i] = 0, fl[i] = -1, dist[i] = inf;
inq[0] = 1;
pre[0] = -1;
fl[0] = inf * 2;
dist[0] = 0;
while(!q.empty()) {
int now = q.front();
q.pop();
inq[now] = 0;
for(int i = la[now]; i != -1; i = edge[i].nxt) {
if(edge[i].flow > 0 && dist[edge[i].to] > dist[now] + edge[i].cost) {
dist[edge[i].to] = dist[now] + edge[i].cost;
fl[edge[i].to] = min(edge[i].flow, fl[now]);
pre[edge[i].to] = now;
e[edge[i].to] = i;
if(!inq[edge[i].to]) {
inq[edge[i].to] = 1;
q.push(edge[i].to);
}
}
}
}
return dist[n + 1] != inf;
}
int main() {
n = read(), m = read();
for(int i = 0; i <= n + 1; ++i) la[i] = -1;
build(0, 1, inf, 0);
build(1, 0, 0, 0);
for(int i = 1; i <= n; ++i) {
int c = read();
build(i, i + 1, inf - c, 0);
build(i + 1, i, 0, 0);
}
for(int i = 1; i <= m; ++i) {
int u = read(), v = read() + 1, c = read();
build(u, v, inf, c);
build(v, u, 0, -c);
}
while(SPFA()) {
int i = n + 1;
maxf = fl[n + 1];
while(i != 0) {
edge[e[i]].flow -= maxf;
edge[e[i] ^ 1].flow += maxf;
i = pre[i];
}
ans += fl[n + 1] * dist[n + 1];
}
printf("%lld\n",ans);
return 0;
}
偷偷更新
这题还可以用线性规划对偶+单纯形算法直接过。