-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASA-Projeto2.cpp
262 lines (210 loc) · 6.43 KB
/
ASA-Projeto2.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*=====================================*/
/* ASA 2018/2019 - Projeto 2 */
/* Joao Dinis 89485 */
/* Tiago Fonseca 89542 */
/*=====================================*/
/* Libraries */
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
// Structs
struct Edge
{
int flow, capacity;
// An edge u--->v has start vertex as u and end
// vertex as v.
int u, v;
Edge(int flow, int capacity, int u, int v)
{
this->flow = flow;
this->capacity = capacity;
this->u = u;
this->v = v;
}
};
struct Vertex
{
int h, e_flow;
Vertex(int h, int e_flow)
{
this->h = h;
this->e_flow = e_flow;
}
};
// Classes
class Graph {
int V; // No. of vertices
vector<Vertex> ver;
vector<Edge> edge;
// Function to push excess flow from u
bool push(int u) {
// Traverse through all edges to find an adjacent (of u)
// to which flow can be pushed
for (int i = 0; i < edge.size(); i++) {
// Checks u of current edge is same as given
// overflowing vertex
if (edge[i].u == u) {
// if flow is equal to capacity then no push
// is possible
if (edge[i].flow == edge[i].capacity)
continue;
// Push is only possible if height of adjacent
// is smaller than height of overflowing vertex
if (ver[u].h > ver[edge[i].v].h) {
// Flow to be pushed is equal to minimum of
// remaining flow on edge and excess flow.
int flow = min(edge[i].capacity - edge[i].flow,
ver[u].e_flow);
// Reduce excess flow for overflowing vertex
ver[u].e_flow -= flow;
// Increase excess flow for adjacent
ver[edge[i].v].e_flow += flow;
// Add residual flow (With capacity 0 and negative
// flow)
edge[i].flow += flow;
updateReverseEdgeFlow(i, flow);
return true;
}
}
}
return false;
}
// Function to relabel a vertex u
void relabel(int u) {
// Initialize minimum height of an adjacent
int mh = INT_MAX;
// Find the adjacent with minimum height
for (int i = 0; i < edge.size(); i++)
{
if (edge[i].u == u)
{
// if flow is equal to capacity then no
// relabeling
if (edge[i].flow == edge[i].capacity)
continue;
// Update minimum height
if (ver[edge[i].v].h < mh)
{
mh = ver[edge[i].v].h;
// updating height of u
ver[u].h = mh + 1;
}
}
}
}
// This function is called to initialize
// preflow
void preflow(int s) {
// Making h of source Vertex equal to no. of vertices
// Height of other vertices is 0.
ver[s].h = ver.size();
//
for (int i = 0; i < edge.size(); i++) {
// If current edge goes from source
if (edge[i].u == s) {
// Flow is equal to capacity
edge[i].flow = edge[i].capacity;
// Initialize excess flow for adjacent v
ver[edge[i].v].e_flow += edge[i].flow;
// Add an edge from v to s in residual graph with
// capacity equal to 0
edge.push_back(Edge(-edge[i].flow, 0, edge[i].v, s));
}
}
}
// Function to reverse edge
void updateReverseEdgeFlow(int i, int flow) {
int u = edge[i].v, v = edge[i].u;
for (int j = 0; j < edge.size(); j++) {
if (edge[j].v == v && edge[j].u == u) {
edge[j].flow -= flow;
return;
}
}
// adding reverse Edge in residual graph
Edge e = Edge(0, flow, u, v);
edge.push_back(e);
}
public:
Graph() {} // Constructor
void graphInit(int V)
{
this->V = V;
// all vertices are initialized with 0 height
// and 0 excess flow
for (int i = 0; i < V; i++)
ver.push_back(Vertex(0, 0));
}
// function to add an edge to graph
void addEdge(int u, int v, int capacity) {
// flow is initialized with 0 for all edge
edge.push_back(Edge(0, capacity, u, v));
}
// returns index of overflowing Vertex
int overFlowVertex(vector<Vertex>& ver, int t) {
for (int i = 1; i < ver.size(); i++)
if ((ver[i].e_flow > 0) & (i != t))
return i;
return -1;
}
// returns maximum flow from s to t
int getMaxFlow(int s, int t) {
preflow(s);
while (overFlowVertex(ver, t) != -1) {
int u = overFlowVertex(ver, t);
if (!push(u))
relabel(u);
}
return ver[t].e_flow;
}
};
/* Prototypes */
/* Variables */
Graph g;
int fornecedores, abastecimento;
void readInput() {
int ligacoes, edge1, edge2, size, totalEdges;
scanf("%d %d %d", &fornecedores, &abastecimento, &ligacoes);
if (fornecedores < 0){
printf("Numero de fornecedores tem que ser maior que 0\n");
exit(-1);
}
if (abastecimento < 0){
printf("Numero de estacoes de abastecimento tem que ser maior ou igual a 0\n");
exit(-1);
}
if (ligacoes < 0){
printf("Numero de ligacoes tem que ser maior ou igual a 0\n");
exit(-1);
}
totalEdges = 2 + fornecedores + 2*abastecimento;
g.graphInit(totalEdges);
for (int i = 0; i < fornecedores; i++) {
scanf("%d ", &size);
edge2 = i + 2;
g.addEdge(0, edge2, size);
}
for (int i = 0; i < abastecimento; i++) {
scanf("%d ", &size);
edge1 = fornecedores + i + 2;
edge2 = edge1 + abastecimento;
g.addEdge(edge1, edge2, size);
}
for (int i = 0; i < ligacoes; i++) {
scanf("%d %d %d", &edge1, &edge2, &size);
if (edge1 >= fornecedores + 2) {
g.addEdge(edge1 + abastecimento, edge2, size);
}
else {
g.addEdge(edge1, edge2, size);
}
}
}
/* Codigo */
int main(int argc, char *argv[]) {
readInput();
int s = 0, t = 1;
printf("%d\n", g.getMaxFlow(s, t));
return 0;
}