-
Notifications
You must be signed in to change notification settings - Fork 2
/
mincost-belman-queue.cpp
81 lines (73 loc) · 1.53 KB
/
mincost-belman-queue.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
const int NEED_FLOW = 1000000000;
typedef double int_e;
struct edge {
int from, to;
int cap;
int_e cost;
int flow;
};
vector<vector<int>> g;
vector<edge> edges;
void add_edge(int from, int to, int_e cost, int cap) {
edge e = { from, to, cap, cost, 0 };
g[from].push_back(edges.size());
edges.push_back(e);
edge e2 = { to, from, 0, -cost, 0 };
g[to].push_back(edges.size());
edges.push_back(e2);
}
pair<int, int_e> mincost(int n, int s, int t) {
int_e cost = 0;
int flow = 0;
while (flow < NEED_FLOW) {
queue<int> q;
q.push(s);
vector<int> in_q(n, 0);
in_q[s] = 1;
vector<int> p(n, -1);
vector<int_e> d(n);
d[s] = 0;
p[s] = s;
while (!q.empty()) {
int v = q.front();
q.pop();
in_q[v] = false;
for (size_t i: g[v]) {
edge& e = edges[i];
if (e.cap == e.flow || p[e.from] == -1)
continue;
if (p[e.to] == -1 || d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
p[e.to] = i;
if (!in_q[e.to]) {
in_q[e.to] = 1;
q.push(e.to);
}
}
}
}
if (p[t] == -1)
break;
//if(d[t] >= 0) { // only for mincost, not mincostmaxflow
// break;
//}
int cur = t;
int maxAdd = NEED_FLOW - flow;
while (cur != s) {
edge& e = edges[p[cur]];
cur = e.from;
maxAdd = min(maxAdd, e.cap - e.flow);
}
flow += maxAdd;
cost += d[t] * maxAdd;
cur = t;
while (cur != s) {
int id = p[cur];
edges[id].flow += maxAdd;
edges[id ^ 1].flow -= maxAdd;
cur = edges[id].from;
}
}
// cost and flow are final here
return make_pair(flow, cost);
}