-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
201 lines (157 loc) · 5.31 KB
/
B.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
#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9) + 5;
// Warning: when choosing flow_t, make sure it can handle the sum of flows, not just individual flows.
template<typename flow_t>
struct dinic {
struct edge {
int node, rev;
flow_t capacity;
edge() {}
edge(int _node, int _rev, flow_t _capacity) : node(_node), rev(_rev), capacity(_capacity) {}
};
int V = -1;
vector<vector<edge>> adj;
vector<int> dist;
vector<int> edge_index;
bool flow_called;
dinic(int vertices = -1) {
if (vertices >= 0)
init(vertices);
}
void init(int vertices) {
V = vertices;
adj.assign(V, {});
dist.resize(V);
edge_index.resize(V);
flow_called = false;
}
void _add_edge(int u, int v, flow_t capacity1, flow_t capacity2) {
assert(0 <= u && u < V && 0 <= v && v < V);
assert(capacity1 >= 0 && capacity2 >= 0);
edge uv_edge(v, int(adj[v].size()) + (u == v ? 1 : 0), capacity1);
edge vu_edge(u, int(adj[u].size()), capacity2);
adj[u].push_back(uv_edge);
adj[v].push_back(vu_edge);
}
void add_directional_edge(int u, int v, flow_t capacity) {
_add_edge(u, v, capacity, 0);
}
void add_bidirectional_edge(int u, int v, flow_t capacity) {
_add_edge(u, v, capacity, capacity);
}
edge &reverse_edge(const edge &e) {
return adj[e.node][e.rev];
}
void bfs_check(queue<int> &q, int node, int new_dist) {
if (new_dist < dist[node]) {
dist[node] = new_dist;
q.push(node);
}
}
bool bfs(int source, int sink) {
dist.assign(V, INF);
queue<int> q;
bfs_check(q, source, 0);
while (!q.empty()) {
int top = q.front(); q.pop();
for (edge &e : adj[top])
if (e.capacity > 0)
bfs_check(q, e.node, dist[top] + 1);
}
return dist[sink] < INF;
}
flow_t dfs(int node, flow_t path_cap, int sink) {
if (node == sink)
return path_cap;
if (dist[node] >= dist[sink])
return 0;
flow_t total_flow = 0;
// Because we are only performing DFS in increasing order of dist, we don't have to revisit fully searched edges
// again later.
while (edge_index[node] < int(adj[node].size())) {
edge &e = adj[node][edge_index[node]];
if (e.capacity > 0 && dist[node] + 1 == dist[e.node]) {
flow_t path = dfs(e.node, min(path_cap, e.capacity), sink);
path_cap -= path;
e.capacity -= path;
reverse_edge(e).capacity += path;
total_flow += path;
}
// If path_cap is 0, we don't want to increment edge_index[node] as this edge may not be fully searched yet.
if (path_cap == 0)
break;
edge_index[node]++;
}
return total_flow;
}
flow_t flow(int source, int sink) {
assert(V >= 0);
flow_t total_flow = 0;
while (bfs(source, sink)) {
edge_index.assign(V, 0);
total_flow += dfs(source, numeric_limits<flow_t>::max(), sink);
}
flow_called = true;
return total_flow;
}
vector<bool> reachable;
void reachable_dfs(int node) {
reachable[node] = true;
for (edge &e : adj[node])
if (e.capacity > 0 && !reachable[e.node])
reachable_dfs(e.node);
}
// Returns a list of {capacity, {from_node, to_node}} representing edges in the min cut.
// TODO: for bidirectional edges, divide the resulting capacities by two.
vector<pair<flow_t, pair<int, int>>> min_cut(int source) {
assert(flow_called);
reachable.assign(V, false);
reachable_dfs(source);
vector<pair<flow_t, pair<int, int>>> cut;
for (int node = 0; node < V; node++)
if (reachable[node])
for (edge &e : adj[node])
if (!reachable[e.node])
cut.emplace_back(reverse_edge(e).capacity, make_pair(node, e.node));
return cut;
}
};
struct Edge {
int64_t from, to, capacity;
Edge() : from(), to(), capacity() {}
Edge(int64_t _from, int64_t _to, int64_t _capacity) : from(_from), to(_to), capacity(_capacity) {}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<Edge> edges;
for(int i=0;i<M;i++) {
int64_t u, v, capacity;
cin >> u >> v >> capacity;
--u, --v;
edges.push_back(Edge(u, v, capacity));
}
int64_t l = 0, r = INF;
while(r > l + 1) {
int64_t m = (l + r) / 2;
dinic<int64_t> graph(2 * N + 2);
int source = 2 * N, sink = 2 * N + 1;
for(int i=0;i<N;i++) {
graph.add_directional_edge(source, i, 1);
graph.add_directional_edge(i + N, sink, 1);
}
for(auto u: edges) {
if(u.capacity <= m) {
graph.add_directional_edge(u.from, u.to + N, u.capacity);
}
}
if(graph.flow(source, sink) == N) {
r = m;
} else {
l = m;
}
}
cout << (r == INF ? -1 : r);
}