-
Notifications
You must be signed in to change notification settings - Fork 2
/
mincostFordBellman.cpp
75 lines (66 loc) · 1.4 KB
/
mincostFordBellman.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
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
const int NEED_FLOW = 1000000000;
typedef long long int_e;
struct edge {
int from, to;
int cap;
int_e cost;
int flow;
};
vector<edge> edges;
void add_edge(int from, int to, int_e cost, int cap) {
edge e = {from, to, cap, cost, 0};
edges.push_back(e);
edge e2 = {to, from, 0, -cost, 0};
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) {
vector<int> p(n, -1);
vector<int_e> d(n);
d[s] = 0;
p[s] = s;
bool changed = true;
while(changed) {
changed = false;
for(size_t i = 0; i < edges.size(); ++i) {
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;
changed = true;
}
}
}
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);
}