「网络流24题」分配问题-题解

题目传送门: 「Luogu P4014」分配问题

题目大意

有$n$件工作要分配给$n$个人做
给出每个人做每个工件的效益$c_{i,j}$

求最小效益和最大效益

题解

裸的最小/大费用最大流
将所有人和所有工作各分为一个点集

  1. 从 源点 向 所有人 建一条 容量为$1$,费用为$0$ 的边(只能选一次,且对答案无贡献)
  2. 从 所有工作 向 汇点 建一条 容量为$1$,费用为$0$ 的边(只能做一次,且对答案无贡献)
  3. 从 每个人 向 所有工作 建一条 容量为$1$,费用为对应效益 的边(只能做一种工作,且对答案工作为对应效益)

求出最小费用最大流和最大费用最大流

两个问之间要清空图并重建(因为求最大流过程中会修改flow)

代码

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
#include <bits/stdc++.h>
using namespace std;

inline int read() {
int x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
return x * f;
}

const int maxn = 310;
const int inf = 0x3f3f3f3f;
const int ninf = 0xc0c0c0c0;

int n, m, s, t, ansflow;
int vis[maxn], d[maxn], p[maxn], a[maxn];
long long anscost;

struct Edge {
int from, to, cap, flow, cost;
Edge(int u, int v, int c, int f, int w): from(u), to(v), cap(c), flow(f), cost(w){}
};
vector<Edge> edges;
vector<int> G[maxn];
void add(int u, int v, int c, int w) {
edges.push_back(Edge(u, v, c, 0, w));
edges.push_back(Edge(v, u, 0, 0,-w));
int mm = edges.size();
G[u].push_back(mm - 2);
G[v].push_back(mm - 1);
}

bool BellmanFord(int& flow, long long& cost, int opt) {
if (opt == 0) memset(d, 0x3f, sizeof(d));
else memset(d, 0xc0, sizeof(d));
memset(vis, 0, sizeof(vis));
d[s] = 0; vis[s] = 1; p[s] = 0; a[s] = inf;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int x = Q.front(); Q.pop();
vis[x] = 0;
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]]; bool judge;
if (opt) judge = d[e.to] < d[x] + e.cost;
else judge = d[e.to] > d[x] + e.cost;
if (e.cap > e.flow && judge) {
d[e.to] = d[x] + e.cost;
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap - e.flow);
if (!vis[e.to]) {
Q.push(e.to);
vis[e.to] = 1;
}
}
}
}
if (!opt && d[t] == inf) return false;
if (opt && d[t] == ninf) return false;
flow += a[t];
cost += (long long)d[t] * (long long)a[t];
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
return true;
}

int MinCostMaxFlow(long long& cost) {
int flow = 0; cost = 0;
while (BellmanFord(flow, cost, 0));
return flow;
}
int MaxCostMaxFlow(long long& cost) {
int flow = 0; cost = 0;
while (BellmanFord(flow, cost, 1));
return flow;
}

int tmp[110][110];

int main() {
n = read(); s = 0; t = n * 2 + 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
int c = read(); tmp[i][j] = c;
add(i, j + n, 1, c);
}
}
for (int i = 1; i <= n; ++i) {
add(s, i, 1, 0);
add(i + n, t, 1, 0);
}
ansflow = MinCostMaxFlow(anscost);
printf("%lld\n", anscost);

edges.clear();
for (int i = 0; i < maxn; ++i) G[i].clear();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
add(i, j + n, 1, tmp[i][j]);
}
}
for (int i = 1; i <= n; ++i) {
add(s, i, 1, 0);
add(i + n, t, 1, 0);
}
ansflow = MaxCostMaxFlow(anscost);
printf("%lld\n", anscost);
return 0;
}

「网络流24题」分配问题-题解

https://blog.tonycrane.cc/p/11b1dcbd.html

作者

TonyCrane

发布于

2020-04-23

更新于

2020-05-05

许可协议