「网络流24题」深海机器人问题-题解

题目传送门: 「Luogu P4012」深海机器人问题

题目大意

一个$P\times Q$网格,机器人可以向北/东走,西南为$(0, 0)$,东北为$(Q, P)$
每个网格边上有生物标本,和价值
有$a$个起点,每个起点有不同的机器人个数
有$b$个终点,每个终点有不同的机器人个数作为目的地

求采集到生物标本的最高总价值

题解

最大费用最大流

  1. 每两个节点之间建一条 容量为$1$,费用为标本价值 的边,和一条 容量为$inf$,费用为$0$ 的边(标本只能采集一次)
  2. 从 源点 向 每个起点 建一条 容量为机器人个数,费用为$0$ 的边
  3. 从 每个终点 向 汇点 建一条 容量为机器人个数,费用为$0$ 的边

跑出最大费用最大流,最大费用即为结果

代码

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
#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 = 410;
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) {
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]];
if (e.cap > e.flow && d[e.to] < d[x] + e.cost) {
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 (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 MaxCostMaxFlow(long long& cost) {
int flow = 0; cost = 0;
while (BellmanFord(flow, cost));
return flow;
}

int p_, q_;
int point(int x, int y) {
return (x - 1) * q_ + y;
}

int main() {
int a_ = read(), b_ = read();
p_ = read() + 1; q_ = read() + 1;
s = 0, t = p_ * q_ + 1;
for (int i = 1; i <= p_; ++i) {
for (int j = 1; j < q_; ++j) {
int w = read();
add(point(i, j), point(i, j + 1), 1, w);
add(point(i, j), point(i, j + 1), inf, 0);
}
}
for (int i = 1; i <= q_; ++i) {
for (int j = 1; j < p_; ++j) {
int w = read();
add(point(j, i), point(j + 1, i), 1, w);
add(point(j, i), point(j + 1, i), inf, 0);
}
}
for (int i = 1; i <= a_; ++i) {
int k = read(), x = read() + 1, y = read() + 1;
add(s, point(x, y), k, 0);
}
for (int i = 1; i <= b_; ++i) {
int r = read(), x = read() + 1, y = read() + 1;
add(point(x, y), t, r, 0);
}
ansflow = MaxCostMaxFlow(anscost);
printf("%d\n", anscost);
return 0;
}

「网络流24题」深海机器人问题-题解

https://blog.tonycrane.cc/p/cfb1d422.html

作者

TonyCrane

发布于

2020-04-22

更新于

2020-05-05

许可协议