Luogu P1791 [国家集训队]人员雇佣

题目描述

https://www.luogu.com.cn/problem/P1791

简要题意:给定 $n$ 个人,现在需要选择若干个人,雇佣第 $i$ 个人需要花费 $a_i$,对于任意两个人 $(i,j)$,如果同时雇佣会得到 $E_{i,j}$ 的收益,如果只雇佣其中一个,则会亏损 $E_{i,j}$,题目保证 $E_{i,j}=E_{j,i}$,求最大收益

$n\le 1000$

Solution

注意到我们的总收入为 $\sum E_{i,j}$,所以我们如果不选 $u$,那么亏损为 $\sum_{i=1}^nE_{u,i}$

如果 $u$ 和 $v$ 一个选一个不选,那么损失为 $2E_{u,v}$

建图:

$s$ 连 $u$,容量为 $\sum_{i=1}^n E_{u,i}$

$u$ 连 $t$,容量为 $a_u$

$u$ 连 $v$,容量为 $2E_{u,v}$

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
#include <iostream>
#include <cstdio>
#include <queue>
#define maxn 1010
#define INF 1000000000
#define ll long long
using namespace std;

int n, m, s, t;

ll a[maxn], b[maxn], c[maxn][maxn], ans;

struct Edge {
int to, next, w;
} e[5000010]; int c1, head[maxn];
inline void add_edge(int u, int v, int w) {
e[c1].to = v; e[c1].w = w;
e[c1].next = head[u]; head[u] = c1++;
}

inline void Add_edge(int u, int v, int w) {
add_edge(u, v, w); add_edge(v, u, 0);
}

int dep[maxn];
bool bfs() {
fill(dep, dep + maxn, 0);
queue<int> Q; Q.push(s); dep[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w;
if (w > 0 && !dep[v]) {
dep[v] = dep[u] + 1;
if (v == t) return 1; Q.push(v);
}
}
}
return 0;
}

int dfs(int u, int _w) {
if (!_w || u == t) return _w;
int s = 0;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to, w = e[i].w;
if (w > 0 && dep[v] == dep[u] + 1) {
int di = dfs(v, min(_w - s, w));
e[i].w -= di; e[i ^ 1]. w += di;
s += di; if (s == _w) break;
}
}
if (s < _w) dep[u] = 0;
return s;
}

int dinic() {
int mf = 0;
while (bfs()) mf += dfs(s, INF);
return mf;
}

int main() { fill(head, head + maxn, -1);
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n; s = 0; t = n + 1;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
cin >> c[i][j];
b[i] += c[i][j]; ans += c[i][j];
}
for (int i = 1; i <= n; ++i) {
Add_edge(s, i, b[i]); Add_edge(i, t, a[i]);
for (int j = 1; j <= n; ++j)
if (i != j) Add_edge(i, j, 2 * c[i][j]);
}
cout << ans - dinic() << "\n";
return 0;
}