Алгоритм Диница: различия между версиями
Перейти к навигации
Перейти к поиску
Ctrlalt (обсуждение | вклад) (Новая страница: « #include <stdio.h> #include <algorithm> #include <vector> #include <queue> using namespace std; struct Edge { int a, b, cap, flow; Edge(int a, i…») |
Ctrlalt (обсуждение | вклад) Нет описания правки |
||
| (не показаны 3 промежуточные версии этого же участника) | |||
| Строка 1: | Строка 1: | ||
class Graph { | |||
struct Edge { | |||
int a, b, capacity, flow = 0; | |||
Edge(int a, int b, int capacity) : | |||
a(a), b(b), capacity(capacity) {} | |||
int other(int v) const { | |||
return v == b ? a : b; | |||
} | |||
int capacityTo(int v) const { | |||
return v == b ? capacity - flow : flow; | |||
} | |||
void addFlowTo(int v, int deltaFlow) { | |||
flow += (v == b ? deltaFlow : -deltaFlow); | |||
vector<Edge> edges; | } | ||
}; | |||
vector<Edge> edges; | |||
vector<vector<int>> graph; | |||
vector<int> dist, edgeTo, index; | |||
bool hasPath(int start, int finish) { | |||
dist.assign(graph.size(), 1e9); | |||
edgeTo.assign(graph.size(), -1); | |||
index.assign(graph.size(), 0); | |||
queue<int> q; | |||
dist[start] = 0; | |||
q.push(start); | |||
while (!q.empty()) { | |||
int v = q.front(); | |||
q.pop(); | |||
for (int edgeIndex : graph[v]) { | |||
int to = edges[edgeIndex].other(v); | |||
if (dist[to] > dist[v] + 1 && edges[edgeIndex].capacityTo(to)) { | |||
dist[to] = dist[v] + 1; | |||
q.push(to); | |||
} | |||
} | } | ||
} | } | ||
return dist[finish] != 1e9; | |||
} | } | ||
bool dfs(int v, int finish) { | |||
if (v == finish) | |||
return 1; | |||
for (; index[v] < graph[v].size(); index[v]++) { | |||
int edgeIndex = graph[v][index[v]], to = edges[edgeIndex].other(v); | |||
if (dist[to] == dist[v] + 1 && edges[edgeIndex].capacityTo(to) && dfs(to, finish)) { | |||
edgeTo[to] = edgeIndex; | |||
return 1; | return 1; | ||
} | |||
} | } | ||
return 0; | |||
} | } | ||
int getMinCapacity(int start, int finish) { | |||
int minCapacity = 1e9; | |||
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) | |||
minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v)); | |||
return minCapacity; | |||
} | } | ||
int | void addFlow(int start, int finish, int deltaFlow) { | ||
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) | |||
edges[edgeTo[v]].addFlowTo(v, deltaFlow); | |||
} | |||
public: | |||
Graph(int vertexCount) : graph(vertexCount) {} | |||
void addEdge(int from, int to, int capacity) { | |||
edges.push_back(Edge(from, to, capacity)); | |||
graph[from].push_back(edges.size() - 1); | |||
graph[to].push_back(edges.size() - 1); | |||
} | |||
long long maxFlow(int start, int finish) { | |||
long long flow = 0; | |||
while (hasPath(start, finish)) { | |||
while (dfs(start, finish)) { | |||
int deltaFlow = getMinCapacity(start, finish); | |||
addFlow(start, finish, deltaFlow); | |||
flow += deltaFlow; | |||
} | |||
} | } | ||
return flow; | |||
} | } | ||
}; | |||
} | |||
== Ссылки == | == Ссылки == | ||
* [http://e-maxx.ru/algo/dinic e-maxx.ru | Теория: | ||
* [http://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D1%85%D0%B5%D0%BC%D0%B0_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D0%B0_%D0%94%D0%B8%D0%BD%D0%B8%D1%86%D0%B0 neerc.ifmo.ru/wiki | * [http://e-maxx.ru/algo/dinic e-maxx.ru — Алгоритм Диница нахождения максимального потока] | ||
* [https://cp-algorithms.com/graph/dinic.html cp-algorithms.com — Maximum flow — Dinic's algorithm] | |||
* [http://neerc.ifmo.ru/wiki/index.php?title=%D0%A1%D1%85%D0%B5%D0%BC%D0%B0_%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D0%B0_%D0%94%D0%B8%D0%BD%D0%B8%D1%86%D0%B0 neerc.ifmo.ru/wiki — Схема алгоритма Диница] | |||
* [https://wiki.algocode.ru/index.php?title=%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%94%D0%B8%D0%BD%D0%B8%D1%86%D0%B0 wiki.algocode.ru — Алгоритм Диница] | |||
* [https://codeforces.com/blog/entry/104960 codeforces.com — My way of understanding Dinic's algorithm] | |||
* [https://codeforces.com/blog/entry/145343 codeforces.com — Worst-Case Graphs for Maximum Flow Algorithms] | |||
* [https://usaco.guide/adv/max-flow?lang=cpp#dinics-algorithm usaco.guide — Dinic's Algorithm] | |||
Демонстрация: | |||
* [https://visualgo.net/en/maxflow visualgo.net — Network Flow] | |||
Код: | |||
* [https://github.com/indy256/codelibrary/blob/master/cpp/graphs/flows/max_flow_dinic.h indy256/codelibrary/cpp/graphs/flows/max_flow_dinic.h] | |||
* [https://github.com/ADJA/algos/blob/master/Graphs/Dinic.cpp ADJA/algos/Graphs/Dinic.cpp] | |||
Задачи: | |||
* [http://informatics.mccme.ru/course/view.php?id=6 informatics.mccme.ru — Курс «Алгоритмы на графах» — часть 9] | * [http://informatics.mccme.ru/course/view.php?id=6 informatics.mccme.ru — Курс «Алгоритмы на графах» — часть 9] | ||
[[Category: Максимальный поток]] | [[Category: Максимальный поток]] | ||
Текущая версия от 19:51, 22 октября 2025
class Graph {
struct Edge {
int a, b, capacity, flow = 0;
Edge(int a, int b, int capacity) :
a(a), b(b), capacity(capacity) {}
int other(int v) const {
return v == b ? a : b;
}
int capacityTo(int v) const {
return v == b ? capacity - flow : flow;
}
void addFlowTo(int v, int deltaFlow) {
flow += (v == b ? deltaFlow : -deltaFlow);
}
};
vector<Edge> edges;
vector<vector<int>> graph;
vector<int> dist, edgeTo, index;
bool hasPath(int start, int finish) {
dist.assign(graph.size(), 1e9);
edgeTo.assign(graph.size(), -1);
index.assign(graph.size(), 0);
queue<int> q;
dist[start] = 0;
q.push(start);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int edgeIndex : graph[v]) {
int to = edges[edgeIndex].other(v);
if (dist[to] > dist[v] + 1 && edges[edgeIndex].capacityTo(to)) {
dist[to] = dist[v] + 1;
q.push(to);
}
}
}
return dist[finish] != 1e9;
}
bool dfs(int v, int finish) {
if (v == finish)
return 1;
for (; index[v] < graph[v].size(); index[v]++) {
int edgeIndex = graph[v][index[v]], to = edges[edgeIndex].other(v);
if (dist[to] == dist[v] + 1 && edges[edgeIndex].capacityTo(to) && dfs(to, finish)) {
edgeTo[to] = edgeIndex;
return 1;
}
}
return 0;
}
int getMinCapacity(int start, int finish) {
int minCapacity = 1e9;
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
minCapacity = min(minCapacity, edges[edgeTo[v]].capacityTo(v));
return minCapacity;
}
void addFlow(int start, int finish, int deltaFlow) {
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
edges[edgeTo[v]].addFlowTo(v, deltaFlow);
}
public:
Graph(int vertexCount) : graph(vertexCount) {}
void addEdge(int from, int to, int capacity) {
edges.push_back(Edge(from, to, capacity));
graph[from].push_back(edges.size() - 1);
graph[to].push_back(edges.size() - 1);
}
long long maxFlow(int start, int finish) {
long long flow = 0;
while (hasPath(start, finish)) {
while (dfs(start, finish)) {
int deltaFlow = getMinCapacity(start, finish);
addFlow(start, finish, deltaFlow);
flow += deltaFlow;
}
}
return flow;
}
};
Ссылки
Теория:
- e-maxx.ru — Алгоритм Диница нахождения максимального потока
- cp-algorithms.com — Maximum flow — Dinic's algorithm
- neerc.ifmo.ru/wiki — Схема алгоритма Диница
- wiki.algocode.ru — Алгоритм Диница
- codeforces.com — My way of understanding Dinic's algorithm
- codeforces.com — Worst-Case Graphs for Maximum Flow Algorithms
- usaco.guide — Dinic's Algorithm
Демонстрация:
Код:
Задачи: