「网络流24题」家园/星际转移问题-题解

题目传送门: 「Luogu P2754」家园/星际转移问题

题目大意

1个地球,$n$个太空站,1个月球,$k$个人
$m$艘船,第$i$艘容纳$h_i$个人,停靠$r_i$站,分别为$S_{i,j}$,每次耗时1
初始人全在地球,结束人全在月球

求最短时间

题解

先根据输入的停靠位置,建立并查集,若地月不连通,则无解,输出$0$

再根据时间建边,时间每增加1,增加一层,求一次最大流,添加进答案
当答案大于$k$时,即可全部到达月球

样例的建图是这样:

代码

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
#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 = 5010;
const int inf = 0x3f3f3f3f;

int n, m, s, t, d[maxn], ans, cur[maxn];
int ufs[25];

int find(int x) {
return (ufs[x] == x) ? x : ufs[x] = find(ufs[x]);
}
void unionn(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) ufs[fx] = fy;
}

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

bool vis[maxn];
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int x = Q.front(); Q.pop();
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}

int DFS(int x, int a) {
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}

int dinic(int s, int t) {
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}

int S[25][25];

int main() {
n = read(); m = read(); int k = read();
s = 0; t = n + 1;
for (int i = 1; i <= n + 1; ++i) ufs[i] = i;
for (int i = 1; i <= m; ++i) {
S[0][i] = read(); S[i][0] = read();
for (int j = 1; j <= S[i][0]; ++j) {
S[i][j] = read();
if (S[i][j] == -1) S[i][j] = n + 1;
if (j > 1) unionn(S[i][j], S[i][j - 1]);
}
}
if (find(s) != find(t)) {
printf("0\n");
return 0;
}
for (int time = 1; ; ++time) {
for (int i = 0; i <= n; ++i) {
add(i + (time - 1) * (n + 2), i + time * (n + 2), inf);
}
add(n + 1 + time * (n + 2), n + 1 + (time - 1) * (n + 2), inf);
for (int i = 1; i <= m; ++i) {
int tmp = (time - 1) % S[i][0] + 1;
add(S[i][tmp] + (time - 1) * (n + 2), S[i][time % S[i][0] + 1] + time * (n + 2), S[0][i]);
}
ans += dinic(s, t);
if (ans >= k) {
printf("%d\n", time);
return 0;
}
}
}

「网络流24题」家园/星际转移问题-题解

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

作者

TonyCrane

发布于

2020-04-14

更新于

2020-05-05

许可协议