/
scalingcirculation.cpp
161 lines (155 loc) · 4.6 KB
/
scalingcirculation.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
#include "scalingcirculation.hpp"
#include "vector.hpp"
#include "staticheap.hpp"
#include <limits>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
void ScalingCirculation::addEdge(int from, int to, int64_t capacity, int64_t cost, int id) {
for (int64_t b = 0; ((int64_t)1<<b) <= capacity; b++) {
if ((int64_t)toAugment.size() <= b) toAugment.push_back(nstd::Vector<int>());
if (capacity & ((int64_t)1<<b)) {
toAugment[b].push_back((int)edges.size());
}
}
edges.push_back({from, to, 0, cost, id});
edges.push_back({to, from, 0, -cost, id});
g[from].push_back((int)edges.size() - 2);
g[to].push_back((int)edges.size() - 1);
}
// Fix potentials to minimal potentials with dijkstra
void ScalingCirculation::fixPotentials() {
nstd::Vector<int64_t> newPotentials(vertices + 1);
for (int i = 1; i <= vertices; i++) {
used[i] = 0;
dist[i] = INFINITE;
}
for (int i = 1; i <= vertices; i++) {
if (used[i] == 0) {
nstd::Vector<int> visited;
dijkstra.putValue(0, i);
dist[i] = 0;
int64_t potAdd = 0;
while (1) {
std::pair<int64_t, int> vertexD = dijkstra.pop();
if (vertexD.first == nstd::StaticHeap::INFINITE) break;
int vertex = vertexD.second;
if (used[vertex]) continue;
used[vertex] = 1;
visited.push_back(vertex);
for (int edgeId : g[vertex]) {
Edge e = edges[edgeId];
int64_t newDistance = dist[vertex] + e.cost + potentials[vertex] - potentials[e.to];
if (e.capacity > 0 && used[e.to] == 0 && newDistance < dist[e.to]) {
dist[e.to] = newDistance;
dijkstra.putValue(newDistance, e.to);
}
}
}
for (int v : visited) {
if (v == i) newPotentials[v] = 0;
else newPotentials[v] = dist[v] - potentials[i] + potentials[v];
for (int edgeId : g[v]) {
Edge e = edges[edgeId];
if (used[e.to] == 2 && e.capacity > 0) {
potAdd = std::max(potAdd, newPotentials[e.to] - e.cost - newPotentials[v]);
}
}
}
for (int v : visited) {
newPotentials[v] += potAdd;
used[v] = 2;
}
}
}
potentials = newPotentials;
}
// Increment capacity of edge
void ScalingCirculation::augmentEdge(int edge) {
edges[edge].capacity++;
if (edges[edge].capacity > 1) return; // Case 1
int64_t pViolate = potentials[edges[edge].from] + edges[edge].cost - potentials[edges[edge].to];
if (pViolate >= 0) return;
for (int i = 1; i <= vertices; i++) {
used[i] = 0;
dist[i] = INFINITE;
fromP[i] = 0;
}
dist[edges[edge].to] = 0;
dijkstra.putValue(0, edges[edge].to);
int64_t negCycle = 0;
while (1) {
std::pair<int64_t, int> vertexD = dijkstra.pop();
if (vertexD.first == nstd::StaticHeap::INFINITE) break;
int vertex = vertexD.second;
if (used[vertex]) continue;
used[vertex] = true;
for (int edgeId : g[vertex]) {
Edge e = edges[edgeId];
int64_t newDistance = dist[vertex] + e.cost + potentials[vertex] - potentials[e.to];
if (e.capacity > 0 && newDistance < dist[e.to]) {
if (e.to == edges[edge].to) {
negCycle = newDistance;
continue;
}
dist[e.to] = newDistance;
fromP[e.to] = edgeId;
dijkstra.putValue(newDistance, e.to);
}
}
}
int64_t maxDist = 0;
for (int i = 1; i <= vertices; i++) {
if (used[i]) maxDist = std::max(maxDist, dist[i]);
}
if (negCycle == 0) { // Case 2/3
for (int i = 1; i <= vertices; i++) {
if (used[i]) potentials[i] += pViolate + dist[i];
else potentials[i] += maxDist;
}
}
else { // Case 4
for (int i = 1; i <= vertices; i++) {
if (used[i]) potentials[i] += dist[i];
else potentials[i] += maxDist;
}
edges[edge].capacity--;
edges[edge^1].capacity++;
int vertex = edges[edge].from;
while (vertex != edges[edge].to) {
edges[fromP[vertex]].capacity--;
edges[fromP[vertex]^1].capacity++;
vertex = edges[fromP[vertex]].from;
}
}
// Fix potentials in order to avoid overflow
fixPotentials();
}
int64_t ScalingCirculation::findMinCostFlow() {
for (int b = (int)toAugment.size() - 1; b >= 0; b--){
// Scaling
for (int i = 0; i < (int)edges.size(); i++) {
edges[i].capacity *= 2;
}
// Increment
for (int edge : toAugment[b]) {
augmentEdge(edge);
}
}
// Compute the final cost
int64_t cost = 0;
for (int i = 1; i < (int)edges.size(); i += 2) {
cost -= edges[i].cost * edges[i].capacity;
}
return cost;
}
nstd::Vector<int64_t> ScalingCirculation::getSolution() {
nstd::Vector<int64_t> ret(edges.size());
for (unsigned i = 0; i < edges.size(); i+=2) {
ret[i/2] = edges[i^1].capacity;
}
return ret;
}
ScalingCirculation::ScalingCirculation(int vertices_) :
vertices(vertices_), g(vertices + 1), potentials(vertices + 1), used(vertices + 1), dist(vertices + 1), dijkstra(vertices + 1), fromP(vertices + 1) {
}