Максимальный поток минимальной стоимости: различия между версиями
Перейти к навигации
Перейти к поиску
Ctrlalt (обсуждение | вклад) Нет описания правки |
Ctrlalt (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
class Edge { | class Graph { | ||
struct Edge { | |||
int a, b, capacity, flow = 0, cost; | |||
Edge(int a, int b, int capacity, int cost) : | |||
a(a), b(b), capacity(capacity), cost(cost) {} | |||
int other(int v) const { | |||
return v == a ? b : a; | |||
} | |||
int capacityTo(int v) const { | |||
return v == b ? capacity - flow : flow; | |||
} | |||
int costTo(int v) const { | |||
return v == b ? cost : -cost; | |||
} | |||
void addFlowTo(int v, int deltaFlow) { | |||
flow += (v == b ? deltaFlow : -deltaFlow); | |||
} | |||
}; | |||
vector<Edge> edges; | vector<Edge> edges; | ||
vector<int> distTo; | vector<int> distTo; | ||
vector<int> edgeTo; | vector<int> edgeTo; | ||
static const int INF = | static const int INF = 1e9; | ||
void fordBellman(int | |||
void fordBellman(int start) { | |||
fill(distTo.begin(), distTo.end(), INF); | |||
distTo[start] = 0; | |||
while (1) { | while (1) { | ||
bool update = 0; | bool update = 0; | ||
for (int i = 0; i < edges.size(); i++) { | for (int i = 0; i < edges.size(); i++) { | ||
int a = edges[i].a | int a = edges[i].a, b = edges[i].b; | ||
if (edges[i].capacityTo(b) && distTo[a] != INF && distTo[b] > distTo[a] + edges[i].costTo(b)) { | if (edges[i].capacityTo(b) && distTo[a] != INF && distTo[b] > distTo[a] + edges[i].costTo(b)) { | ||
distTo[b] = distTo[a] + edges[i].costTo(b); | distTo[b] = distTo[a] + edges[i].costTo(b); | ||
| Строка 48: | Строка 50: | ||
} | } | ||
} | } | ||
bool hasPath(int | |||
bool hasPath(int start, int finish) { | |||
fordBellman(start); | |||
fordBellman( | return distTo[finish] != INF; | ||
return distTo[ | |||
} | } | ||
int bottleneckCapacity(int | |||
int bottleneckCapacity(int start, int finish) { | |||
int bCapacity = INF; | int bCapacity = INF; | ||
for (int v = | for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) | ||
bCapacity = min(bCapacity, edges[edgeTo[v]].capacityTo(v)); | bCapacity = min(bCapacity, edges[edgeTo[v]].capacityTo(v)); | ||
return bCapacity; | return bCapacity; | ||
} | } | ||
long long addFlow(int | |||
long long addFlow(int start, int finish, int deltaFlow) { | |||
long long deltaCost = 0; | long long deltaCost = 0; | ||
for (int v = | for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) { | ||
edges[edgeTo[v]].addFlowTo(v, | edges[edgeTo[v]].addFlowTo(v, deltaFlow); | ||
deltaCost += | deltaCost += deltaFlow * edges[edgeTo[v]].costTo(v); | ||
} | } | ||
return deltaCost; | return deltaCost; | ||
} | } | ||
public: | public: | ||
Graph(int | Graph(int vertexCount) : | ||
distTo | distTo(vertexCount), edgeTo(vertexCount) {} | ||
void addEdge(int from, int to, int capacity, int cost) { | void addEdge(int from, int to, int capacity, int cost) { | ||
edges.push_back(Edge(from, to, capacity, cost)); | edges.push_back(Edge(from, to, capacity, cost)); | ||
} | } | ||
pair<long long, long long> minCostMaxFlow(int | |||
pair<long long, long long> minCostMaxFlow(int start, int finish) { | |||
long long cost = 0, flow = 0; | long long cost = 0, flow = 0; | ||
while (hasPath( | while (hasPath(start, finish)) { | ||
int deltaFlow = bottleneckCapacity( | int deltaFlow = bottleneckCapacity(start, finish); | ||
cost += addFlow( | cost += addFlow(start, finish, deltaFlow); | ||
flow += deltaFlow; | flow += deltaFlow; | ||
} | } | ||
return | return { cost, flow }; | ||
} | } | ||
}; | }; | ||
Текущая версия от 17:51, 25 июня 2023
class Graph {
struct Edge {
int a, b, capacity, flow = 0, cost;
Edge(int a, int b, int capacity, int cost) :
a(a), b(b), capacity(capacity), cost(cost) {}
int other(int v) const {
return v == a ? b : a;
}
int capacityTo(int v) const {
return v == b ? capacity - flow : flow;
}
int costTo(int v) const {
return v == b ? cost : -cost;
}
void addFlowTo(int v, int deltaFlow) {
flow += (v == b ? deltaFlow : -deltaFlow);
}
};
vector<Edge> edges;
vector<int> distTo;
vector<int> edgeTo;
static const int INF = 1e9;
void fordBellman(int start) {
fill(distTo.begin(), distTo.end(), INF);
distTo[start] = 0;
while (1) {
bool update = 0;
for (int i = 0; i < edges.size(); i++) {
int a = edges[i].a, b = edges[i].b;
if (edges[i].capacityTo(b) && distTo[a] != INF && distTo[b] > distTo[a] + edges[i].costTo(b)) {
distTo[b] = distTo[a] + edges[i].costTo(b);
edgeTo[b] = i;
update = 1;
}
if (edges[i].capacityTo(a) && distTo[b] != INF && distTo[a] > distTo[b] + edges[i].costTo(a)) {
distTo[a] = distTo[b] + edges[i].costTo(a);
edgeTo[a] = i;
update = 1;
}
}
if (!update)
break;
}
}
bool hasPath(int start, int finish) {
fordBellman(start);
return distTo[finish] != INF;
}
int bottleneckCapacity(int start, int finish) {
int bCapacity = INF;
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v))
bCapacity = min(bCapacity, edges[edgeTo[v]].capacityTo(v));
return bCapacity;
}
long long addFlow(int start, int finish, int deltaFlow) {
long long deltaCost = 0;
for (int v = finish; v != start; v = edges[edgeTo[v]].other(v)) {
edges[edgeTo[v]].addFlowTo(v, deltaFlow);
deltaCost += deltaFlow * edges[edgeTo[v]].costTo(v);
}
return deltaCost;
}
public:
Graph(int vertexCount) :
distTo(vertexCount), edgeTo(vertexCount) {}
void addEdge(int from, int to, int capacity, int cost) {
edges.push_back(Edge(from, to, capacity, cost));
}
pair<long long, long long> minCostMaxFlow(int start, int finish) {
long long cost = 0, flow = 0;
while (hasPath(start, finish)) {
int deltaFlow = bottleneckCapacity(start, finish);
cost += addFlow(start, finish, deltaFlow);
flow += deltaFlow;
}
return { cost, flow };
}
};
Ссылки
Теория:
- e-maxx.ru — Поток минимальной стоимости (min-cost-flow). Алгоритм увеличивающих путей
- neerc.ifmo.ru/wiki — Поиск потока минимальной стоимости методом дополнения вдоль путей минимальной стоимости
Код:
- CodeLibrary — Maximum flow of minimum cost with Bellman–Ford
- CodeLibrary — Maximum flow of minimum cost with potentials for dense graphs
- CodeLibrary — Maximum flow of minimum cost with potentials
- Algos — Min Cost Flow (or Min Cost Max Flow) algorithm with Ford-Bellman algorithm as shortest path search method
- Algos — Min Cost Flow (or Min Cost Max Flow) algorithm with Dijkstra algorithm (with potentials) as shortest path search method. (Dijkstra for dense graphs running in O(N^2))
- Algos — Min Cost Flow (or Min Cost Max Flow) algorithm with Dijkstra algorithm (with potentials) as shortest path search method. (Dijkstra on heap for sparse graphs)
Задачи: