Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
375 lines (325 loc) · 8.13 KB
/
Copy pathmain.cpp
File metadata and controls
375 lines (325 loc) · 8.13 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "bits/stdc++.h"
#define ALPHA 3
#define OMEGA 3
#define LIMITE_T 0.1
#define TOP 1
#define A 200.0
using namespace std;
void debug(string message)
{
cout << message << endl;
}
// Definição do grafo
vector<vector<int>> matriz_adj;
vector<vector<pair<int, int>>> lista_adj;
// Variáveis necessárias criação da solução
vector<int> visitados;
int linhas_a, linhas_b;
random_device rd;
// Váriaveis que descrevem a solução atual
vector<pair<int, int>> resultados;
vector<int> caminho;
struct Solucao
{
int melhor_resultado, idx, qtd_vertices, tempo;
vector<int> caminho;
vector<pair<int, int>> resultados_parciais;
Solucao();
Solucao(vector<int> &caminho_, vector<pair<int, int>> &resultados_);
int funcao_objetiva(int custo_ida, int custo_volta, int q_vertices);
void trocar_ida(int a, int b);
void trocar_volta(int a, int b);
void reposicionar_pares(int idx_a, int idx_b);
void recalc_resultado();
void obter_tempo(clock_t inicio);
void obter_melhor_resultado();
void print_resultado();
void print_melhor_caminho();
void print_resultado_detalhado();
};
bool bl_linhas_a(Solucao &atual);
bool bl_linhas_b(Solucao &atual);
void bl_nojump(Solucao &atual, int lado);
void obter_ordem(vector<pair<int, int>> &v);
int obter_linha_inicial(int linhas);
void dfs(int v, int custo_ida, int custo_volta, bool ida);
Solucao grasp(int n, int linha_inicial);
// O(P*N³)
int main(int argc, char *argv[])
{
double inicio = clock();
int n, m, temp;
cin >> linhas_a >> linhas_b;
n = linhas_a + linhas_b;
m = linhas_a * linhas_b * 2;
lista_adj.assign(n, vector<pair<int, int>>());
matriz_adj.assign(n, vector<int>(n, 0));
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
lista_adj[a].push_back({c, b});
matriz_adj[a][b] = c;
}
vector<Solucao> solucoes;
while ((clock() - inicio) / (CLOCKS_PER_SEC * 1.0) < LIMITE_T)
{
int linha_inicial = obter_linha_inicial(linhas_a);
Solucao a = grasp(n, 0);
a.obter_tempo(inicio);
auto b = a;
bl_nojump(a, 0);
while (bl_linhas_b(a))
{
a.obter_tempo(inicio);
}
bl_nojump(b, 1);
while (bl_linhas_a(b))
{
b.obter_tempo(inicio);
}
if (a.melhor_resultado > b.melhor_resultado)
solucoes.push_back(a);
else
solucoes.push_back(b);
}
sort(solucoes.begin(), solucoes.end(),
[](Solucao &x, Solucao &y)
{
if (x.melhor_resultado == y.melhor_resultado)
return x.tempo < y.tempo;
return x.melhor_resultado > y.melhor_resultado;
});
cout << argv[1] << ": ";
solucoes.front().print_resultado();
// for (int i = 0; i < TOP; i++)
// {
// solucoes[i].print_resultado_detalhado();
// solucoes[i].print_melhor_caminho();
// }
}
// O(1)
Solucao::Solucao() {}
// O(N)
Solucao::Solucao(vector<int> &caminho_, vector<pair<int, int>> &resultados_)
{
this->caminho = caminho_;
this->resultados_parciais = resultados_;
obter_melhor_resultado();
}
// O(1)
void Solucao::trocar_ida(int idx_a, int idx_b)
{
swap(this->caminho[idx_a * 2], this->caminho[idx_b * 2]);
}
// O(1)
void Solucao::trocar_volta(int idx_a, int idx_b)
{
swap(this->caminho[idx_a * 2 + 1], this->caminho[idx_b * 2 + 1]);
}
// O(1)
void Solucao::reposicionar_pares(int idx_a, int idx_b)
{
swap(this->caminho[idx_a * 2], this->caminho[idx_b * 2]);
swap(this->caminho[idx_a * 2 + 1], this->caminho[idx_b * 2 + 1]);
}
// O(1)
int Solucao::funcao_objetiva(int custo_ida, int custo_volta, int q_vertices)
{
return A * q_vertices - (custo_ida + custo_volta);
}
// O(n)
void Solucao::recalc_resultado()
{
pair<int, int> custo = {0, 0};
for (int i = 0; i < this->caminho.size() - 1; i++)
{
if (i % 2 == 0)
{
custo.first += matriz_adj[this->caminho[i]][this->caminho[i + 1]];
this->resultados_parciais[i / 2 + 1] = custo;
}
else
{
custo.second += matriz_adj[this->caminho[i]][this->caminho[i + 1]];
}
}
// for(auto it: resultados_parciais){
// cout << "(" << it.first << ',' << it.second << ")" << ' ';
// }
// cout << endl;
obter_melhor_resultado();
}
// O(N)
void Solucao::obter_melhor_resultado()
{
this->qtd_vertices = 0;
this->melhor_resultado = 0;
for (int i = 0; i < this->resultados_parciais.size(); i++)
{
int resultado_atual = funcao_objetiva(
this->resultados_parciais[i].first,
this->resultados_parciais[i].second,
2 * i);
if (resultado_atual > this->melhor_resultado)
{
this->idx = i;
this->qtd_vertices = 2 * i;
this->melhor_resultado = resultado_atual;
}
}
}
// O(1)
void Solucao::print_resultado()
{
cout << setprecision(5) << fixed << this->melhor_resultado / (A * this->qtd_vertices)
<< " | " << setw(8) <<this->tempo << " ms" << endl;
}
// O(1)
void Solucao::print_resultado_detalhado()
{
cout << "Vertices: " << this->qtd_vertices << endl
<< "Função Objetiva: " << this->melhor_resultado << endl
<< "Resultado: " << setprecision(5) << fixed << this->melhor_resultado / (A * this->qtd_vertices) << endl
<< "Tempo: " << this->tempo << " ms" << endl;
}
// O(N)
void Solucao::print_melhor_caminho()
{
cout << "Caminho:" << endl;
cout << "Linhas A -";
for (int i = 0; i < this->resultados_parciais.size() - 1; i++)
cout << setw(3) << this->caminho[i * 2] << ' ';
cout << endl;
cout << "Linhas B -";
for (int i = 0; i < this->resultados_parciais.size() - 1; i++)
cout << setw(3) << this->caminho[i * 2 + 1] << ' ';
cout << endl;
}
// O(1)
void Solucao::obter_tempo(clock_t inicio)
{
this->tempo = 1000 * (clock() - inicio) / (CLOCKS_PER_SEC * 1.0);
}
// O(N³)
bool bl_linhas_a(Solucao &atual)
{
Solucao nova = atual;
vector<pair<int, int>> ordem(nova.resultados_parciais.size() - 1);
int melhor_resultado = nova.melhor_resultado;
bool flag = false;
obter_ordem(ordem);
for (int i = 0; i < ordem.size(); i++)
{
int a = ordem[i].second;
for (int j = i + 1; j < ordem.size(); j++)
{
int b = ordem[j].second;
nova.trocar_ida(a, b);
nova.recalc_resultado();
if (nova.melhor_resultado > atual.melhor_resultado)
{
flag = true;
atual = nova;
}
else
{
nova.trocar_ida(a, b);
}
}
}
return false;
}
// O(N³)
bool bl_linhas_b(Solucao &atual)
{
Solucao nova = atual;
vector<pair<int, int>> ordem(nova.resultados_parciais.size() - 1);
int melhor_resultado = nova.melhor_resultado;
bool flag = false;
obter_ordem(ordem);
for (int i = 0; i < ordem.size(); i++)
{
int a = ordem[i].second;
for (int j = i + 1; j < ordem.size(); j++)
{
int b = ordem[j].second;
nova.trocar_volta(a, b);
nova.recalc_resultado();
if (nova.melhor_resultado > atual.melhor_resultado)
{
flag = true;
atual = nova;
}
else
{
nova.trocar_volta(b, a);
}
}
}
return flag;
}
// O(N²)
void bl_nojump(Solucao &atual, int lado)
{
for (int i = 0; i < atual.resultados_parciais.size() - 1; i++)
{
for (int j = 1; j < atual.resultados_parciais.size() - 1 - i; j++)
{
if (atual.caminho[j * 2 + lado] > atual.caminho[(j - 1) * 2 + lado])
continue;
atual.reposicionar_pares(j - 1, j);
}
}
atual.recalc_resultado();
}
// O(log(N)*N)
int obter_linha_inicial(int linhas)
{
vector<pair<int, int>> opcoes;
for (int i = 0; i < linhas; i++)
{
opcoes.push_back({i * 5 + rd() % 300, i});
}
sort(opcoes.begin(), opcoes.end());
return opcoes.front().second;
}
// O(log(N)*N)
void obter_ordem(vector<pair<int, int>> &v)
{
for (int i = 0; i < v.size(); i++)
v[i] = {i + rd() % int(OMEGA * v.size()), i};
sort(v.begin(), v.end());
}
// O(log(N)*N²)
void dfs(int v, int custo_ida, int custo_volta, bool ida)
{
vector<pair<int, int>> melhores;
visitados[v] = 1;
caminho.push_back(v);
for (auto it : lista_adj[v])
{
if (visitados[it.second])
continue;
melhores.push_back(it);
}
if (!melhores.size())
return;
sort(melhores.begin(), melhores.end());
int escolhido = rd() % (melhores.size() < ALPHA ? melhores.size() : ALPHA);
custo_ida += melhores[escolhido].first * ida;
custo_volta += melhores[escolhido].first * (!ida);
v = melhores[escolhido].second;
if (ida)
resultados.push_back({custo_ida, custo_volta});
dfs(v, custo_ida, custo_volta, !ida);
}
// O(log(N)*N²)
Solucao grasp(int n, int linha_inicial)
{
visitados.assign(n, 0);
resultados = vector<pair<int, int>>(1, {0, 0});
caminho.clear();
dfs(linha_inicial, 0, 0, true);
return Solucao(caminho, resultados);
}