-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravelling_salesman.cpp
80 lines (61 loc) · 1.79 KB
/
travelling_salesman.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
/* Universidade de Brasília
* Departamento de Ciência da Computação
* CIC0258 - Tópicos Especiais em Programação Competitiva
* Prof. Dr. Vinicius R. P. Borges
*
* Aula: Programacao Dinamica (Problema do Caixeiro Viajante) - Travelling Salesman Problem
*
* Complexidade: O(n 2^n)
*
* Compilar: g++ travelling_salesman.cpp -std=c++17 -o caixeiro
* Executar: ./caixeiro
*/
#include<bits/stdc++.h>
#define oo 2e9
#define MAX 100000
using namespace std;
typedef long long ll;
int n=4;
int dist[5][5] = {
{ 0, 0, 0, 0, 0 }, { 0, 0, 10, 15, 20 },
{ 0, 10, 0, 25, 25 }, { 0, 15, 25, 0, 30 },
{ 0, 20, 25, 30, 0 },
};
ll tab[7][MAX+2];
int travelling_salesman(int current_city, int bitmask){
int next_city,ans = oo;
//printf("current_city %d bitmask %d\n",current_city,bitmask);
if (bitmask == ((1 << current_city) | 3))
return dist[1][current_city];
if (tab[current_city][bitmask] != 0)
return tab[current_city][bitmask];
for(int i = 1; i <= n; i++){
if( (bitmask & (1<<i)) and i != current_city and i != 1){
next_city = travelling_salesman(i, bitmask & (~(1<<current_city)));
ans = min(ans,next_city+dist[i][current_city]);
}
}
tab[current_city][bitmask] = ans;
return ans;
}
int main(){
// grafo com n vertices
//scanf("%d %d",&n,&m);
for(int i = 0; i < 7;i++){
for(int j = 0; j <= MAX; j++){
tab[i][j] = 0;
}
}
// Calcula a matriz de distancias
/*for(int i = 1; i <= n;i++){
for(int j = 1; j <= n; j++){
D[i][j] = ;
}
}*/
int ans = oo;
for(int i = 1; i <= n; i++){
ans = min(ans,travelling_salesman(i,(1 << (n+1))-1)+dist[i][1]);
}
printf("%d\n",ans);
return 0;
}