Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 108d6fb

Browse files
authored
Update Dinic.cpp
1 parent 6f6e8a0 commit 108d6fb

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

Graphs/NetworkFlow/Dinic.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,46 @@
33
* Tiempo: O(V^2 E)
44
*/
55

6+
template<typename T>
67
struct Dinic {
8+
#define INF numeric_limits<T>::max()
79
struct Edge {
810
int to, rev;
9-
ll c, oc;
10-
ll flow() { return max(oc - c, 0LL); } // if you need flows
11+
T c, oc;
12+
T flow() { return max(oc - c, T(0)); } // if you need flows
1113
};
1214
vi lvl, ptr, q;
1315
vector<vector<Edge>> adj;
1416
Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
15-
void addEdge(int a, int b, ll c, ll rcap = 0) {
17+
void addEdge(int a, int b, T c, T rcap = 0) {
1618
adj[a].push_back({b, SZ(adj[b]), c, c});
1719
adj[b].push_back({a, SZ(adj[a]) - 1, rcap, rcap});
1820
}
19-
ll dfs(int v, int t, ll f) {
21+
T dfs(int v, int t, T f) {
2022
if (v == t || !f) return f;
2123
for (int& i = ptr[v]; i < SZ(adj[v]); i++) {
2224
Edge& e = adj[v][i];
2325
if (lvl[e.to] == lvl[v] + 1)
24-
if (ll p = dfs(e.to, t, min(f, e.c))) {
26+
if (T p = dfs(e.to, t, min(f, e.c))) {
2527
e.c -= p, adj[e.to][e.rev].c += p;
2628
return p;
2729
}
2830
}
2931
return 0;
3032
}
31-
ll calc(int s, int t) {
32-
ll flow = 0;
33+
T calc(int s, int t) {
34+
T flow = 0;
3335
q[0] = s;
34-
FOR(L, 0, 31)
35-
do { // 'int L=30' maybe faster for random data
36+
FOR(L, 0, 31) do { // 'int L=30' maybe faster for random data
3637
lvl = ptr = vi(SZ(q));
3738
int qi = 0, qe = lvl[s] = 1;
3839
while (qi < qe && !lvl[t]) {
3940
int v = q[qi++];
40-
for (Edge e : adj[v])
41-
if (!lvl[e.to] && e.c >> (30 - L))
42-
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
41+
for (Edge e : adj[v]) if (!lvl[e.to] && e.c >> (30 - L)) q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
4342
}
44-
while (ll p = dfs(s, t, LLONG_MAX)) flow += p;
43+
while (T p = dfs(s, t, INF)) flow += p;
4544
}
46-
while (lvl[t])
47-
;
45+
while (lvl[t]);
4846
return flow;
4947
}
5048
bool leftOfMinCut(int a) { return lvl[a] != 0; }

0 commit comments

Comments
 (0)