-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmax_flow_dinic.cpp
313 lines (273 loc) · 9.46 KB
/
max_flow_dinic.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
// max-flow (Dinic's algorithm)
//
// verified
// AOJ Course GRL_6_A Network Flow - Maximum Flow
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_6_A&lang=jp
//
// AtCoder Library Practice Contest D - Maxflow
// https://atcoder.jp/contests/practice2/tasks/practice2_d
//
// ABC 259 G - Grid Card Game
// https://atcoder.jp/contests/abc259/tasks/abc259_g
//
#include <bits/stdc++.h>
using namespace std;
// edge class (for network-flow)
template<class FLOWTYPE> struct FlowEdge {
// core members
int rev, from, to;
FLOWTYPE cap, icap, flow;
// constructor
FlowEdge(int r, int f, int t, FLOWTYPE c)
: rev(r), from(f), to(t), cap(c), icap(c), flow(0) {}
void reset() { cap = icap, flow = 0; }
// debug
friend ostream& operator << (ostream& s, const FlowEdge& E) {
return s << E.from << "->" << E.to << '(' << E.flow << '/' << E.icap << ')';
}
};
// graph class (for network-flow)
template<class FLOWTYPE> struct FlowGraph {
// core members
vector<vector<FlowEdge<FLOWTYPE>>> list;
vector<pair<int,int>> pos; // pos[i] := {vertex, order of list[vertex]} of i-th edge
// constructor
FlowGraph(int n = 0) : list(n) { }
void init(int n = 0) {
list.assign(n, FlowEdge<FLOWTYPE>());
pos.clear();
}
// getter
vector<FlowEdge<FLOWTYPE>> &operator [] (int i) {
return list[i];
}
const vector<FlowEdge<FLOWTYPE>> &operator [] (int i) const {
return list[i];
}
size_t size() const {
return list.size();
}
FlowEdge<FLOWTYPE> &get_rev_edge(const FlowEdge<FLOWTYPE> &e) {
if (e.from != e.to) return list[e.to][e.rev];
else return list[e.to][e.rev + 1];
}
FlowEdge<FLOWTYPE> &get_edge(int i) {
return list[pos[i].first][pos[i].second];
}
const FlowEdge<FLOWTYPE> &get_edge(int i) const {
return list[pos[i].first][pos[i].second];
}
vector<FlowEdge<FLOWTYPE>> get_edges() const {
vector<FlowEdge<FLOWTYPE>> edges;
for (int i = 0; i < (int)pos.size(); ++i) {
edges.push_back(get_edge(i));
}
return edges;
}
// change edges
void reset() {
for (int i = 0; i < (int)list.size(); ++i) {
for (FlowEdge<FLOWTYPE> &e : list[i]) e.reset();
}
}
void change_edge(FlowEdge<FLOWTYPE> &e, FLOWTYPE new_cap, FLOWTYPE new_flow) {
FlowEdge<FLOWTYPE> &re = get_rev_edge(e);
e.cap = new_cap - new_flow, e.icap = new_cap, e.flow = new_flow;
re.cap = new_flow;
}
// add_edge
void add_edge(int from, int to, FLOWTYPE cap) {
pos.emplace_back(from, (int)list[from].size());
list[from].push_back(FlowEdge<FLOWTYPE>((int)list[to].size(), from, to, cap));
list[to].push_back(FlowEdge<FLOWTYPE>((int)list[from].size() - 1, to, from, 0));
}
// debug
friend ostream& operator << (ostream& s, const FlowGraph &G) {
const auto &edges = G.get_edges();
for (const auto &e : edges) s << e << endl;
return s;
}
};
// Dinic
template<class FLOWTYPE> FLOWTYPE Dinic
(FlowGraph<FLOWTYPE> &G, int s, int t, FLOWTYPE limit_flow)
{
FLOWTYPE current_flow = 0;
vector<int> level((int)G.size(), -1), iter((int)G.size(), 0);
// Dinic BFS
auto bfs = [&]() -> void {
level.assign((int)G.size(), -1);
level[s] = 0;
queue<int> que;
que.push(s);
while (!que.empty()) {
int v = que.front();
que.pop();
for (const FlowEdge<FLOWTYPE> &e : G[v]) {
if (level[e.to] < 0 && e.cap > 0) {
level[e.to] = level[v] + 1;
if (e.to == t) return;
que.push(e.to);
}
}
}
};
// Dinic DFS
auto dfs = [&](auto self, int v, FLOWTYPE up_flow) {
if (v == t) return up_flow;
FLOWTYPE res_flow = 0;
for (int &i = iter[v]; i < (int)G[v].size(); ++i) {
FlowEdge<FLOWTYPE> &e = G[v][i], &re = G.get_rev_edge(e);
if (level[v] >= level[e.to] || e.cap == 0) continue;
FLOWTYPE flow = self(self, e.to, min(up_flow - res_flow, e.cap));
if (flow <= 0) continue;
res_flow += flow;
e.cap -= flow, e.flow += flow;
re.cap += flow, re.flow -= flow;
if (res_flow == up_flow) break;
}
return res_flow;
};
// flow
while (current_flow < limit_flow) {
bfs();
if (level[t] < 0) break;
iter.assign((int)iter.size(), 0);
while (current_flow < limit_flow) {
FLOWTYPE flow = dfs(dfs, s, limit_flow - current_flow);
if (!flow) break;
current_flow += flow;
}
}
return current_flow;
};
template<class FLOWTYPE> FLOWTYPE Dinic(FlowGraph<FLOWTYPE> &G, int s, int t) {
return Dinic(G, s, t, numeric_limits<FLOWTYPE>::max());
}
//------------------------------//
// Examples
//------------------------------//
// AOJ
void AOJ_Course_GRL_6_A() {
int V, E;
cin >> V >> E;
FlowGraph<int> G(V);
for (int i = 0; i < E; ++i) {
int u, v, c;
cin >> u >> v >> c;
G.add_edge(u, v, c);
}
int res = Dinic(G, 0, V-1);
cout << res << endl;
}
// ACL practice D
void ACL_practice_D() {
// 上下左右を表すベクトル
const vector<int> DX = {1, 0, -1, 0};
const vector<int> DY = {0, 1, 0, -1};
// 入力受け取り
int N, M;
cin >> N >> M;
vector<string> grid(N);
for (int i = 0; i < N; ++i) cin >> grid[i];
// フローネットワークを作る
// 各マスの番号を 0, 1, ..., NM-1 とし、超頂点の番号を S = NM, T = NM+1 とする
FlowGraph<int> G(N * M + 2);
int S = N * M, T = N * M + 1;
// マス (i, j) の頂点番号を返す関数
auto index = [&](int i, int j) -> int { return i * M + j; };
// 黒色マスと白色マスを結ぶ (黒色:i + j が偶数、白色:i + j が奇数)
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
// 黒色マスならば、上下左右の 4 マスと辺を結んでいく
if ((i + j) % 2 == 0 && grid[i][j] == '.') {
for (int dir = 0; dir < 4; ++dir) {
int i2 = i + DX[dir], j2 = j + DY[dir];
if (i2 < 0 || i2 >= N || j2 < 0 || j2 >= M) continue;
// どちらも空マスならば、ドミノを置けるので、辺を結ぶ
if (grid[i2][j2] == '.') {
G.add_edge(index(i, j), index(i2, j2), 1);
}
}
}
// 超頂点 S から黒色マスへの辺を結ぶ
if ((i + j) % 2 == 0 && grid[i][j] == '.') {
G.add_edge(S, index(i, j), 1);
}
// 白色マスから超頂点 T への辺を結ぶ
if ((i + j) % 2 == 1 && grid[i][j] == '.') {
G.add_edge(index(i, j), T, 1);
}
}
}
// 最大流を流す
int max_flow = Dinic(G, S, T);
// フロー値が 1 となった辺を特定して、ドミノタイリングを復元する
const auto &edges = G.get_edges();
for (const auto &e : edges) {
// 辺 e が超頂点に接続するものや、フロー値が 0 であるものはスキップ
if (e.from == S || e.to == T || e.flow == 0) continue;
// 辺 e の両端点に対応するマス
int ifrom = e.from / M, jfrom = e.from % M;
int ito = e.to / M, jto = e.to % M;
// ドミノを置く
if (ifrom == ito) {
// ドミノを横に配置する場合
if (jfrom > jto) swap(jfrom, jto);
grid[ifrom][jfrom] = '>';
grid[ito][jto] = '<';
} else if (jfrom == jto) {
// ドミノを縦に配置する場合
if (ifrom > ito) swap(ifrom, ito);
grid[ifrom][jfrom] = 'v';
grid[ito][jto] = '^';
}
}
// 出力
cout << max_flow << endl;
for (int i = 0; i < N; ++i) cout << grid[i] << endl;
}
// ABC 259 G
void ABC_259_G() {
const long long INF = 1LL<<50;
// 入力
int H, W;
cin >> H >> W;
vector<vector<long long>> A(H, vector<long long>(W));
for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) {
cin >> A[i][j];
A[i][j] = -A[i][j];
}
long long B = 0;
vector<long long> S(H + W, 0);
for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) S[i] += A[i][j];
for (int j = 0; j < W; ++j) for (int i = 0; i < H; ++i) S[j+H] += A[i][j];
for (int i = 0; i < H + W; ++i) B = min(B, S[i]);
B = -B;
// グラフを構築
int source = H + W, sink = H + W + 1;
FlowGraph<long long> G(H + W + 2);
for (int i = 0; i < H; ++i) {
G.add_edge(source, i, B);
G.add_edge(i, sink, B + S[i]);
}
for (int j = 0; j < W; ++j) {
G.add_edge(source, j+H, B + S[j+H]);
G.add_edge(j+H, sink, B);
}
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
long long cost = (A[i][j] <= 0 ? -A[i][j] : INF);
G.add_edge(i, j+H, cost);
}
}
long long flow = Dinic(G, source, sink);
long long res = -(flow - B * (H + W));
cout << res << endl;
}
int main() {
//AOJ_Course_GRL_6_A();
ACL_practice_D();
//ABC_259_G();
}