-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ adds maximum flow dinic's algorithm
- Loading branch information
iagorrr
committed
Mar 25, 2024
1 parent
840ee94
commit a3d5e75
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
struct Dinic { | ||
struct Edge { | ||
int to, rev; | ||
long long c, oc; | ||
long long flow() { return max(oc - c, 0LL); } // if you need flows | ||
}; | ||
vector<int> lvl, ptr, q; | ||
vector<vector<Edge>> adj; | ||
Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {} | ||
void addEdge(int a, int b, long long c, long long rcap = 0) { | ||
adj[a].push_back({b, (int)adj[b].size(), c, c}); | ||
adj[b].push_back({a, (int)adj[a].size() - 1, rcap, rcap}); | ||
} | ||
long long dfs(int v, int t, long long f) { | ||
if (v == t || !f) return f; | ||
for (int& i = ptr[v]; i < (int)adj[v].size(); i++) { | ||
Edge& e = adj[v][i]; | ||
if (lvl[e.to] == lvl[v] + 1) | ||
if (long long p = dfs(e.to, t, min(f, e.c))) { | ||
e.c -= p, adj[e.to][e.rev].c += p; | ||
return p; | ||
} | ||
} | ||
return 0; | ||
} | ||
long long calc(int s, int t) { | ||
long long flow = 0; q[0] = s; | ||
for (int L = 0; L < 31; L++) | ||
do { // 'int L=30' maybe faster for random data | ||
lvl = ptr = vector<int>(q.size()); | ||
int qi = 0, qe = lvl[s] = 1; | ||
while (qi < qe && !lvl[t]) { | ||
int v = q[qi++]; | ||
for (Edge e : adj[v]) | ||
if (!lvl[e.to] && e.c >> (30 - L)) | ||
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1; | ||
} | ||
while (long long p = dfs(s, t, LLONG_MAX)) flow += p; | ||
} while (lvl[t]); | ||
return flow; | ||
} | ||
bool leftOfMinCut(int a) { return lvl[a] != 0; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
\subsection{Maximum Flow (Dinic)} | ||
|
||
Finds the \textbf{maximum flow} in a graph network, given the \textbf{source $s$} and the \textbf{sink $t$}. Add edge from $a$ to $b$ with capcity $c$. | ||
|
||
Complexity: time in general $O(E \cdot V^2)$, if every capacity is 1, and every vertice has in degree equal 1 or out degree equal 1 then $O(E \cdot \sqrt{V})$, | ||
|
||
Suffle the edges list for every vertice may take you out of the worst case |