-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
213 lines (175 loc) · 4.93 KB
/
graph.cpp
File metadata and controls
213 lines (175 loc) · 4.93 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Sergeev Artemiy, 33601/2
* Dinic's algorithm
*/
#include <queue>
#include <limits.h>
#include "graph.h"
using namespace flow;
network_t::neighbour_list_t::iterator network_t::placeFor( unsigned int id, unsigned int idWhere )
{
neighbour_list_t::iterator iter = _vertices[idWhere].begin(),
end = _vertices[idWhere].end();
/* Search a place for new edge */
while (iter != end)
{
if (iter->_id >= id)
return iter;
++iter;
}
return iter;
}
int network_t::addEdge( unsigned int idFrom, unsigned int idTo, int capacity )
{
int oldCapacity = -1;
if (idFrom < _verticesCount && idTo < _verticesCount && capacity >= 0)
{
/* Insert forward edge */
neighbour_t neigh1(idTo, capacity);
neighbour_list_t::iterator iter1 = placeFor(idTo, idFrom);
if (iter1 != _vertices[idFrom].end() && iter1->_id == idTo)
{
oldCapacity = iter1->_capacity;
iter1->_capacity = capacity;
}
else
{
oldCapacity = 0;
_vertices[idFrom].insert(iter1, neigh1);
}
/* Insert inverse edge with zero capacity */
neighbour_t neigh2(idFrom, 0);
neighbour_list_t::iterator iter2 = placeFor(idFrom, idTo);
if (iter2 != _vertices[idTo].end() && iter2->_id == idFrom)
iter2->_capacity = 0;
else
_vertices[idTo].insert(iter2, neigh2);
_edgesCount += 2;
}
return oldCapacity;
}
int network_t::deleteEdge( unsigned int idFrom, unsigned int idTo )
{
int deletedCapacity = -1;
if (idFrom < _verticesCount && idTo < _verticesCount)
{
struct is_equal
{
unsigned int _id;
int _deletedCapacity;
is_equal( unsigned int id ) : _id(id), _deletedCapacity(-1) { }
bool operator()( const neighbour_t &neigh )
{
if (neigh._id == _id)
{
_deletedCapacity = neigh._capacity;
return true;
}
return false;
}
};
unsigned int size = _vertices[idFrom].size();
is_equal predictor(idFrom);
_vertices[idFrom].remove_if(predictor);
deletedCapacity = predictor._deletedCapacity;
_edgesCount += _vertices[idFrom].size() - size;
size = _vertices[idTo].size();
_vertices[idTo].remove_if(is_equal(idFrom));
_edgesCount += _vertices[idTo].size() - size;
}
return deletedCapacity;
}
int network_t::setEdgeCapacity( unsigned int idFrom, unsigned int idTo, int capacity )
{
int oldCapacity = -1;
if (idFrom < _verticesCount && idTo < _verticesCount)
{
neighbour_list_t::iterator iter = placeFor(idFrom, idTo);
if (iter != _vertices[idFrom].end() && iter->_id == idTo)
{
oldCapacity = iter->_capacity;
iter->_capacity = capacity;
}
}
return oldCapacity;
}
int network_t::getEdgeCapacity( unsigned int idFrom, unsigned int idTo )
{
if (idFrom < _verticesCount && idTo < _verticesCount)
{
neighbour_list_t::iterator iter = placeFor(idFrom, idTo);
if (iter != _vertices[idFrom].end() && iter->_id == idTo)
return iter->_capacity;
}
return 0;
}
bool network_t::dinicBFS( int *distances )
{
/* Set distances for '-1' (unexpected) value */
std::fill<int *>(distances, distances + _verticesCount, -1);
/* BFS queue */
std::queue<unsigned int> Q;
distances[_source] = 0;
Q.push(_source);
do
{
unsigned int index = Q.front();
Q.pop();
neighbour_list_t::iterator iter = _vertices[index].begin(),
end = _vertices[index].end();
while (iter != end)
{
if (distances[iter->_id] < 0 && iter->_flow < iter->_capacity)
{
distances[iter->_id] = distances[index] + 1;
Q.push(iter->_id);
}
++iter;
}
} while (!Q.empty() && distances[_sink] < 0);
return distances[_sink] >= 0;
return false;
}
int network_t::dinicDFS( unsigned int id, int flow, unsigned int *pointers, int *distances )
{
if (!flow)
return 0;
if (id == _sink)
return flow;
/* Get first neighbour to visit */
neighbour_list_t::iterator iter = _vertices[id].begin();
for (unsigned int i = 0; i < pointers[id]; ++i)
++iter;
/* Visit neighbours loop */
for (; pointers[id] < _vertices[id].size(); ++pointers[id], ++iter)
{
if (distances[iter->_id] == distances[id] + 1 && iter->_flow < iter->_capacity)
{
int pushed = dinicDFS(iter->_id, std::min<int>(flow, iter->_capacity - iter->_flow), pointers, distances );
if (pushed)
{
iter->_flow += pushed;
iter = placeFor(id, iter->_id);
iter->_flow -= pushed;
return pushed;
}
}
}
return 0;
}
int network_t::maximumFlow( void )
{
int maxFlow = 0;
int *distances = new int[_verticesCount];
unsigned int *pointers = new unsigned int[_verticesCount];
clear();
while (dinicBFS(distances))
{
std::fill(pointers, pointers + _verticesCount, '\0');
while (int pushed = dinicDFS(_source, INT_MAX, pointers, distances))
maxFlow += pushed;
}
delete[] distances;
delete[] pointers;
return maxFlow;
}