-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSP.cpp
84 lines (82 loc) · 1.79 KB
/
TSP.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
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int x;
int y;
}node;
int hillclimb_TSP(node *location,int **map, int &city_num,int dest,int source){
unsigned int total_cost = 10000,cost;
int delta_cost;
node current[city_num];
int i,j,x,y,loop = 1000,pre;
srand(1);
while(loop > 0){
for(i = 0,current[0].x = source,cost = 0;;){
x = current[i].x;
pre = 0;
if(map[x][dest] == 100){
while(1){
y = rand()%city_num;
if(map[x][y] < 100 && y != x && y != pre) break;
}
//cout<<y<<endl;
cost += map[x][y];
current[i].y = y;
current[++i].x = y;
pre = x;
}
else break;
}
current[i].y = dest;
cost += map[x][dest];
delta_cost = total_cost - cost;
if(delta_cost > 0 ){
total_cost = cost;
for(j = 0;j <= i;j++){
location[j].x = current[j].x;
location[j].y = current[j].y;
}
}
loop--;
}
cout<<"path is "<<endl;
for(j = 0; j<= i; j++){
cout<<location[j].x+1 <<" --> ";
}
cout<<location[i].y+1<<endl;
return total_cost;
}
int main(){
int city_num;
int i,j;
int cost,dest,source;
cout<<"please input the number of cities"<<endl;
cin>>city_num;
cout<<"destination is "<<endl;
cin>>dest;
cout<<"source is "<<endl;
cin>>source;
node *location = new node[city_num];
int **map = new int*[city_num];
cout<<"please input the map"<<endl;
for(i = 0; i< city_num; i++){
map[i] = new int[city_num];
}
for(i = 0; i< city_num; i++){
for(j = i;j < city_num;j++){
cout<<"length between the city "<<i+1<<" and the city "<<j+1<<endl;
cin>>map[i][j];
map[j][i] = map[i][j];
}
}
for(i = 0;i < city_num;i++){
for(j = 0;j < city_num;j++){
cout <<map[i][j]<<" ";
}
cout<<endl;
}
cost = hillclimb_TSP(location,map,city_num,dest-1,source-1);
cout<<endl<<"cost is "<<cost<<endl;
return 0;
}