-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
94 lines (74 loc) · 2.25 KB
/
main.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
#include <iostream>
#include "grafo.h"
#include "caminosimple.h"
int main() {
int n, m, n1, n2;
// leo el grafo desde stdin
std::cin >> n;
Grafo g(n);
std::cin >> m;
while (m--) {
std::cin >> n1 >> n2;
g.agregarArista(n1, n2);
}
// std::cout << g;
// genero solucion inicial
CaminoSimple c;
Grafo::Nodo v = 0;
c.agregarNodo(v);
Grafo::Vecinos ws = g.vecinos(v);
Grafo::IteradorVecinos iter = ws.begin();
while (iter != ws.end()) {
if(c.agregarNodo(*iter)) {
ws = g.vecinos(*iter);
iter = ws.begin();
} else ++iter;
}
// std::cout << "Tamaño de camino inicial: ";
// std::cout << c.cantNodos() << std::endl;
// std::cout << "Camino inicial: ";
// c.mostrarCamino(std::cout);
int indice;
bool hayMejora = true;
while (hayMejora) {
// busco en la vecindad si existe una mejor solucion
indice = 0;
hayMejora = false;
while (indice < c.cantNodos()) {
CaminoSimple solucionVecina(c);
// elimino los ultimos (n-indice+1) nodos del camino
Grafo::Nodo ultimoNodoEliminado;
while (solucionVecina.cantNodos() > indice+1) {
ultimoNodoEliminado = solucionVecina.borrarUltimo();
}
int x = c.iesimo(indice);
Grafo::Vecinos vs = g.vecinos(x);
Grafo::IteradorVecinos iter = vs.begin();
// trato de agrandar el camino a partir de "indice" pero por
// un nodo distinto al ultimo eliminado.
while (iter != vs.end()) {
if (*iter != ultimoNodoEliminado && solucionVecina.agregarNodo(*iter)) {
ultimoNodoEliminado = -1;
vs = g.vecinos(*iter);
iter = vs.begin();
} else ++iter;
}
// actualizo la mejor solucion
if (solucionVecina.cantNodos() > c.cantNodos()) {
c = solucionVecina;
hayMejora = true;
// std::cout << "\tTamaño de mejor vecino: ";
// std::cout << c.cantNodos()<< std::endl;
// std::cout << "\tMejor vecino encontrado:";
// c.mostrarCamino(std::cout);
}
indice++;
}
}
// por ultimo muestro la solucion encontrada
// std::cout << "Tamaño de camino final: ";
// std::cout << c.cantNodos() << std::endl;
// std::cout << "Camino encontrado: ";
c.mostrarCamino(std::cout);
return 0;
}