-
Notifications
You must be signed in to change notification settings - Fork 2
/
Minimum cost flow.cpp
169 lines (142 loc) · 3.4 KB
/
Minimum cost flow.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*8<
@Title: Minimum Cost Flow
@Description:
Given a network find the minimum cost to
achieve a flow of at most $f$. Works with
\textbf{directed} and \textbf{undirected}
graphs
@Usage:
\begin{compactitem}
\item \textbf{add(u, v, c, w):} adds an
edge from $u$ to $v$ with capacity $c$
and cost $w$.
\item \textbf{flow(f):} return a pair
$(flow, cost)$ with the maximum flow
until $f$ with source at $s$ and sink at
$t$, with the minimum cost possible.
\end{compactitem}
@Time:
$O(N \cdot M + f \cdot m \log{n})$
>8*/
template <typename T>
struct MinCostFlow {
struct Edge {
int to;
ll c, rc; // capcity, residual capacity
T w; // cost
};
int n, s, t;
vector<Edge> edges;
vi2d g;
vector<T> dist;
vi pre;
MinCostFlow() {}
MinCostFlow(int n_, int _s, int _t)
: n(n_), s(_s), t(_t), g(n) {}
void addEdge(int u, int v, ll c, T w) {
g[u].pb(len(edges));
edges.eb(v, c, 0, w);
g[v].pb(len(edges));
edges.eb(u, 0, 0, -w);
}
// {flow, cost}
pair<ll, T> flow(ll flow_limit = LLONG_MAX) {
ll flow = 0;
T cost = 0;
while (flow < flow_limit and dijkstra(s, t)) {
ll aug = LLONG_MAX;
for (int i = t; i != s;
i = edges[pre[i] ^ 1].to) {
aug = min({flow_limit - flow, aug,
edges[pre[i]].c});
}
for (int i = t; i != s;
i = edges[pre[i] ^ 1].to) {
edges[pre[i]].c -= aug;
edges[pre[i] ^ 1].c += aug;
edges[pre[i]].rc += aug;
edges[pre[i] ^ 1].rc -= aug;
}
flow += aug;
cost += (T)aug * dist[t];
}
return {flow, cost};
}
// Needs to be called after flow method
vi2d paths() {
vi2d p;
for (;;) {
int cur = s;
auto &res = p.eb();
res.pb(cur);
while (cur != t) {
bool found = false;
for (auto i : g[cur]) {
auto &[v, _, c, cost] = edges[i];
if (c > 0) {
--c;
res.pb(cur = v);
found = true;
break;
}
}
if (!found) break;
}
if (cur != t) {
p.ppb();
break;
}
}
return p;
}
private:
bool bellman_ford(int s, int t) {
dist.assign(n, numeric_limits<T>::max());
pre.assign(n, -1);
vc inq(n, false);
queue<int> q;
dist[s] = 0;
q.push(s);
while (len(q)) {
int u = q.front();
q.pop();
inq[u] = false;
for (int i : g[u]) {
auto [v, c, w, _] = edges[i];
auto new_dist = dist[u] + w;
if (c > 0 and dist[v] > new_dist) {
dist[v] = new_dist;
pre[v] = i;
if (not inq[v]) {
inq[v] = true;
q.push(v);
}
}
}
}
return dist[t] != numeric_limits<T>::max();
}
bool dijkstra(int s, int t) {
dist.assign(n, numeric_limits<T>::max());
pre.assign(n, -1);
dist[s] = 0;
using PQ = pair<T, int>;
pqmn<PQ> pq;
pq.emp(0, s);
while (len(pq)) {
auto [cost, u] = pq.top();
pq.pop();
if (cost != dist[u]) continue;
for (int i : g[u]) {
auto [v, c, _, w] = edges[i];
auto new_dist = dist[u] + w;
if (c > 0 and dist[v] > new_dist) {
dist[v] = new_dist;
pre[v] = i;
pq.emp(new_dist, v);
}
}
}
return dist[t] != numeric_limits<T>::max();
}
};