Skip to content

Added Dinic #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions content/graphs/Dinic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* Description: Max flow algorithm. Can find a valid
* circulation given vertex and/or edge demands. Time:
* $O(VE\log{U})$
*/

struct Dinic {
// disable scaling when max flow/capacity is small, or
// sometimes on random data
const static bool SCALING = true;
struct Edge {
int v, dual;
ll cap, res;
constexpr ll flow() { return max(cap - res, 0ll); }
};
int n, s, t;
vector<int> lvl, q, blk;
vector<vector<Edge>> adj;
vector<pair<int, int>> edges;
Dinic(int n): n(n + 2), s(n++), t(n++), q(n), adj(n) {}
int add(int u, int v, ll cap, ll flow = 0) {
adj[u].push_back({v, int(adj[v].size()), cap, cap - flow});
adj[v].push_back({u, int(adj[u].size()) - 1, 0, 0});
edges.emplace_back(u, adj[u].size() - 1);
return edges.size() - 1; // this Edge's ID
}
ll dfs(int u, ll in) {
if (u == t || !in) return in;
ll flow = 0;
for (auto& e : adj[u]) {
if (e.res && !blk[e.v] && lvl[e.v] == lvl[u] - 1)
if (ll out = dfs(e.v, min(in, e.res))) {
flow += out, in -= out, e.res -= out;
adj[e.v][e.dual].res += out;
if (!in) return flow;
}
}
blk[u] = 1;
return flow;
}
ll flow() {
ll flow = 0;
q[0] = t;
for (int B = SCALING * 30; B >= 0; B--) do {
lvl = blk = vector<int>(n);
int qi = 0, qe = lvl[t] = 1;
while (qi < qe && !lvl[s]) {
int u = q[qi++];
for (auto& e : adj[u])
if (!lvl[e.v] && adj[e.v][e.dual].res >> B)
q[qe++] = e.v, lvl[e.v] = lvl[u] + 1;
}
if (lvl[s]) flow += dfs(s, LLONG_MAX);
} while (lvl[s]);
return flow;
}
Edge& get(int id) { // get Edge object from its ID
return adj[edges[id].first][edges[id].second];
}
void clear() {
for (auto& it : adj)
for (auto& e : it) e.res = e.cap;
}
bool leftOfMinCut(int u) { return lvl[u] == 0; }
// d is a list of vertex demands, d[u] = flow in - flow out
// negative if u is a source, positive if u is a sink
bool circulation(vector<ll> d = {}) {
d.resize(n);
vector<int> circEdges;
Dinic g(n);
for (int u = 0; u < n; u++)
for (auto& e : adj[u]) {
d[u] += e.flow(), d[e.v] -= e.flow();
if (e.res) circEdges.push_back(g.add(u, e.v, e.res));
}
int tylerEdge = g.add(t, s, LLONG_MAX, 0);
ll flow = 0;
for (int u = 0; u < n; u++)
if (d[u] < 0)
g.add(g.s, u, -d[u]);
else if (d[u] > 0)
g.add(u, g.t, d[u]), flow += d[u];
if (flow != g.flow()) return false;
int i = 0; // reconstruct the flow into this graph
for (int u = 0; u < n; u++)
for (auto& e : adj[u])
if (e.res) e.res -= g.get(circEdges[i++]).flow();
return true;
}
};
1 change: 1 addition & 0 deletions content/graphs/chapter.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ \chapter{Graphs}
% \kactlimport{Dijkstra.h} % probably shouldn't be printed
\kactlimport{SCCTarjan.h}
\kactlimport{SCCKosaraju.h}
\kactlimport{Dinic.h}
Binary file modified kactl.pdf
Binary file not shown.
92 changes: 92 additions & 0 deletions snippets/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,98 @@
"hack_Dijkstra"
]
},
"Dinic.h": {
"body": [
"struct Dinic {",
"\t// disable scaling when max flow/capacity is small, or",
"\t// sometimes on random data",
"\tconst static bool SCALING = true;",
"\tstruct Edge {",
"\t\tint v, dual;",
"\t\tll cap, res;",
"\t\tconstexpr ll flow() { return max(cap - res, 0ll); }",
"\t};",
"\tint n, s, t;",
"\tvector<int> lvl, q, blk;",
"\tvector<vector<Edge>> adj;",
"\tvector<pair<int, int>> edges;",
"\tDinic(int n) : n(n + 2), s(n++), t(n++), q(n), adj(n) {}",
"\tint add(int u, int v, ll cap, ll flow = 0) {",
"\t\tadj[u].push_back({v, int(adj[v].size()), cap, cap - flow});",
"\t\tadj[v].push_back({u, int(adj[u].size()) - 1, 0, 0});",
"\t\tedges.emplace_back(u, adj[u].size() - 1);",
"\t\treturn edges.size() - 1; // this Edge's ID",
"\t}",
"\tll dfs(int u, ll in) {",
"\t\tif (u == t || !in) return in;",
"\t\tll flow = 0;",
"\t\tfor (auto& e : adj[u]) {",
"\t\t\tif (e.res && !blk[e.v] && lvl[e.v] == lvl[u] - 1)",
"\t\t\t\tif (ll out = dfs(e.v, min(in, e.res))) {",
"\t\t\t\t\tflow += out, in -= out, e.res -= out;",
"\t\t\t\t\tadj[e.v][e.dual].res += out;",
"\t\t\t\t\tif (!in) return flow;",
"\t\t\t\t}",
"\t\t}",
"\t\tblk[u] = 1;",
"\t\treturn flow;",
"\t}",
"\tll flow() {",
"\t\tll flow = 0;",
"\t\tq[0] = t;",
"\t\tfor (int B = SCALING * 30; B >= 0; B--) do {",
"\t\t\t\tlvl = blk = vector<int>(n);",
"\t\t\t\tint qi = 0, qe = lvl[t] = 1;",
"\t\t\t\twhile (qi < qe && !lvl[s]) {",
"\t\t\t\t\tint u = q[qi++];",
"\t\t\t\t\tfor (auto& e : adj[u])",
"\t\t\t\t\t\tif (!lvl[e.v] && adj[e.v][e.dual].res >> B)",
"\t\t\t\t\t\t\tq[qe++] = e.v, lvl[e.v] = lvl[u] + 1;",
"\t\t\t\t}",
"\t\t\t\tif (lvl[s]) flow += dfs(s, LLONG_MAX);",
"\t\t\t} while (lvl[s]);",
"\t\treturn flow;",
"\t}",
"\tEdge& get(int id) { // get Edge object from its ID",
"\t\treturn adj[edges[id].first][edges[id].second];",
"\t}",
"\tvoid clear() {",
"\t\tfor (auto& it : adj)",
"\t\t\tfor (auto& e : it) e.res = e.cap;",
"\t}",
"\tbool leftOfMinCut(int u) { return lvl[u] == 0; }",
"\t// d is a list of vertex demands, d[u] = flow in - flow out",
"\t// negative if u is a source, positive if u is a sink",
"\tbool circulation(vector<ll> d = {}) {",
"\t\td.resize(n);",
"\t\tvector<int> circEdges;",
"\t\tDinic g(n);",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tfor (auto& e : adj[u]) {",
"\t\t\t\td[u] += e.flow(), d[e.v] -= e.flow();",
"\t\t\t\tif (e.res) circEdges.push_back(g.add(u, e.v, e.res));",
"\t\t\t}",
"\t\tint tylerEdge = g.add(t, s, LLONG_MAX, 0);",
"\t\tll flow = 0;",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tif (d[u] < 0)",
"\t\t\t\tg.add(g.s, u, -d[u]);",
"\t\t\telse if (d[u] > 0)",
"\t\t\t\tg.add(u, g.t, d[u]), flow += d[u];",
"\t\tif (flow != g.flow()) return false;",
"\t\tint i = 0; // reconstruct the flow into this graph",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tfor (auto& e : adj[u])",
"\t\t\t\tif (e.res) e.res -= g.get(circEdges[i++]).flow();",
"\t\treturn true;",
"\t}",
"};"
],
"description": "Max flow algorithm. Can find a valid\ncirculation given vertex and/or edge demands. Time:\n$O(VE\\log{U})$",
"prefix": [
"hack_Dinic"
]
},
"Fraction.h": {
"body": [
"template <typename T>",
Expand Down
94 changes: 94 additions & 0 deletions snippets/cpp/Dinic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"Dinic.h": {
"body": [
"struct Dinic {",
"\t// disable scaling when max flow/capacity is small, or",
"\t// sometimes on random data",
"\tconst static bool SCALING = true;",
"\tstruct Edge {",
"\t\tint v, dual;",
"\t\tll cap, res;",
"\t\tconstexpr ll flow() { return max(cap - res, 0ll); }",
"\t};",
"\tint n, s, t;",
"\tvector<int> lvl, q, blk;",
"\tvector<vector<Edge>> adj;",
"\tvector<pair<int, int>> edges;",
"\tDinic(int n) : n(n + 2), s(n++), t(n++), q(n), adj(n) {}",
"\tint add(int u, int v, ll cap, ll flow = 0) {",
"\t\tadj[u].push_back({v, int(adj[v].size()), cap, cap - flow});",
"\t\tadj[v].push_back({u, int(adj[u].size()) - 1, 0, 0});",
"\t\tedges.emplace_back(u, adj[u].size() - 1);",
"\t\treturn edges.size() - 1; // this Edge's ID",
"\t}",
"\tll dfs(int u, ll in) {",
"\t\tif (u == t || !in) return in;",
"\t\tll flow = 0;",
"\t\tfor (auto& e : adj[u]) {",
"\t\t\tif (e.res && !blk[e.v] && lvl[e.v] == lvl[u] - 1)",
"\t\t\t\tif (ll out = dfs(e.v, min(in, e.res))) {",
"\t\t\t\t\tflow += out, in -= out, e.res -= out;",
"\t\t\t\t\tadj[e.v][e.dual].res += out;",
"\t\t\t\t\tif (!in) return flow;",
"\t\t\t\t}",
"\t\t}",
"\t\tblk[u] = 1;",
"\t\treturn flow;",
"\t}",
"\tll flow() {",
"\t\tll flow = 0;",
"\t\tq[0] = t;",
"\t\tfor (int B = SCALING * 30; B >= 0; B--) do {",
"\t\t\t\tlvl = blk = vector<int>(n);",
"\t\t\t\tint qi = 0, qe = lvl[t] = 1;",
"\t\t\t\twhile (qi < qe && !lvl[s]) {",
"\t\t\t\t\tint u = q[qi++];",
"\t\t\t\t\tfor (auto& e : adj[u])",
"\t\t\t\t\t\tif (!lvl[e.v] && adj[e.v][e.dual].res >> B)",
"\t\t\t\t\t\t\tq[qe++] = e.v, lvl[e.v] = lvl[u] + 1;",
"\t\t\t\t}",
"\t\t\t\tif (lvl[s]) flow += dfs(s, LLONG_MAX);",
"\t\t\t} while (lvl[s]);",
"\t\treturn flow;",
"\t}",
"\tEdge& get(int id) { // get Edge object from its ID",
"\t\treturn adj[edges[id].first][edges[id].second];",
"\t}",
"\tvoid clear() {",
"\t\tfor (auto& it : adj)",
"\t\t\tfor (auto& e : it) e.res = e.cap;",
"\t}",
"\tbool leftOfMinCut(int u) { return lvl[u] == 0; }",
"\t// d is a list of vertex demands, d[u] = flow in - flow out",
"\t// negative if u is a source, positive if u is a sink",
"\tbool circulation(vector<ll> d = {}) {",
"\t\td.resize(n);",
"\t\tvector<int> circEdges;",
"\t\tDinic g(n);",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tfor (auto& e : adj[u]) {",
"\t\t\t\td[u] += e.flow(), d[e.v] -= e.flow();",
"\t\t\t\tif (e.res) circEdges.push_back(g.add(u, e.v, e.res));",
"\t\t\t}",
"\t\tint tylerEdge = g.add(t, s, LLONG_MAX, 0);",
"\t\tll flow = 0;",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tif (d[u] < 0)",
"\t\t\t\tg.add(g.s, u, -d[u]);",
"\t\t\telse if (d[u] > 0)",
"\t\t\t\tg.add(u, g.t, d[u]), flow += d[u];",
"\t\tif (flow != g.flow()) return false;",
"\t\tint i = 0; // reconstruct the flow into this graph",
"\t\tfor (int u = 0; u < n; u++)",
"\t\t\tfor (auto& e : adj[u])",
"\t\t\t\tif (e.res) e.res -= g.get(circEdges[i++]).flow();",
"\t\treturn true;",
"\t}",
"};"
],
"description": "Max flow algorithm. Can find a valid\ncirculation given vertex and/or edge demands. Time:\n$O(VE\\log{U})$",
"prefix": [
"hack_Dinic"
]
}
}
33 changes: 33 additions & 0 deletions testing/graphs/Dinic_FASTFLOW.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// tested on SPOJ FASTFLOW
// https://www.spoj.com/problems/FASTFLOW/
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

#include "../../content/graphs/Dinic.h"

int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);

int n, m;
cin >> n >> m;

Dinic g(n);
g.add(g.s, 0, LLONG_MAX / 4);
g.add(n - 1, g.t, LLONG_MAX / 4);

for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;

g.add(a, b, c);
g.add(b, a, c);
}

cout << g.flow() << '\n';

return 0;
}
34 changes: 34 additions & 0 deletions testing/graphs/Dinic_MATCHING.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// tested on SPOJ MATCHING
// https://www.spoj.com/problems/MATCHING/
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

#include "../../content/graphs/Dinic.h"

int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);

int n1, n2, m;
cin >> n1 >> n2 >> m;

Dinic g(n1 + n2);

for (int i = 0; i < n1; i++) g.add(g.s, i, 1);

for (int i = 0; i < n2; i++) g.add(n1 + i, g.t, 1);

for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;

g.add(u, n1 + v, 1);
}

cout << g.flow() << '\n';

return 0;
}
39 changes: 39 additions & 0 deletions testing/graphs/Dinic_ReactorCooling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// tested on ASC 1: Reactor Cooling
// https://codeforces.com/gym/100199
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

#include "../../content/graphs/Dinic.h"

int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
freopen("cooling.in", "r", stdin);
freopen("cooling.out", "w", stdout);

int n, m;
cin >> n >> m;

Dinic g(n);
vector<int> ids;

for (int i = 0; i < m; i++) {
int u, v, d, c;
cin >> u >> v >> d >> c;
u--, v--;

ids.push_back(g.add(u, v, c, d));
}

if (!g.circulation()) {
cout << "NO\n";
return 0;
}

cout << "YES\n";
for (int id : ids) cout << g.get(id).flow() << '\n';

return 0;
}