-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinCostMaxFlow.cpp
107 lines (94 loc) · 2.54 KB
/
MinCostMaxFlow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <bits/stdc++.h>
using namespace std;
// Source: https://cp-algorithms.com/graph/min_cost_flow.html
const int INF = 1e9;
struct Edge {
int from, to, capacity, cost;
};
struct Graph {
int N;
vector<Edge> edges;
vector<vector<int>> adj, cost, capacity;
Graph(int N_) : N(N_){};
// Cost is PER UNIT OF FLOW!
void AddEdge(int from, int to, int capacity, int cost) {
assert(0 <= from && from < N && 0 <= to && to < N);
edges.push_back(Edge{from, to, capacity, cost});
}
void ShortestPaths(int n, int v0, vector<int>& d, vector<int>& p) {
d.assign(n, INF);
d[v0] = 0;
vector<bool> inq(n, false);
queue<int> q;
q.push(v0);
p.assign(n, -1);
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = false;
for (int v : adj[u]) {
if (capacity[u][v] > 0 && d[v] > d[u] + cost[u][v]) {
d[v] = d[u] + cost[u][v];
p[v] = u;
if (!inq[v]) {
inq[v] = true;
q.push(v);
}
}
}
}
}
// Finds max flow s->t with flow <=K, of those - flow with min cost.
// If K=INF, finds minimum-cost maximum-flow.
// Returns (flow, cost).
pair<int, int> MinCostFlow(int K, int s, int t) {
assert(0 <= s && s < N && 0 <= t && t < N);
adj.assign(N, vector<int>());
cost.assign(N, vector<int>(N, 0));
capacity.assign(N, vector<int>(N, 0));
for (Edge e : edges) {
adj[e.from].push_back(e.to);
adj[e.to].push_back(e.from);
cost[e.from][e.to] = e.cost;
cost[e.to][e.from] = -e.cost;
capacity[e.from][e.to] = e.capacity;
}
int flow = 0;
int cost = 0;
vector<int> d, p;
while (flow < K) {
ShortestPaths(N, s, d, p);
if (d[t] == INF) break;
// Find max flow on that path.
int f = K - flow;
int cur = t;
while (cur != s) {
f = min(f, capacity[p[cur]][cur]);
cur = p[cur];
}
// Apply flow.
flow += f;
cost += f * d[t];
cur = t;
while (cur != s) {
capacity[p[cur]][cur] -= f;
capacity[cur][p[cur]] += f;
cur = p[cur];
}
}
return make_pair(flow, cost);
}
};
int main() {
Graph gr(4);
gr.AddEdge(0, 1, 2, 0);
gr.AddEdge(1, 2, 3, 1);
gr.AddEdge(0, 2, 6, 100);
gr.AddEdge(0, 3, 3, 0);
gr.AddEdge(3, 2, 2, 10);
assert(gr.MinCostFlow(0, 0, 2) == make_pair(0, 0));
assert(gr.MinCostFlow(2, 0, 2) == make_pair(2, 2));
assert(gr.MinCostFlow(5, 0, 2) == make_pair(5, 122));
assert(gr.MinCostFlow(INF, 0, 2) == make_pair(10, 622));
return 0;
}