-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTravelling_salesman.cpp
More file actions
85 lines (60 loc) · 1.02 KB
/
Travelling_salesman.cpp
File metadata and controls
85 lines (60 loc) · 1.02 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
#include<iostream>
using namespace std;
int a[100][100],seen[10],n,cost=0;
int least_cost(int v)
{
int i,nc=9999;
int min=9999,k_min;
for(i=0;i < n;i++)
{
if((a[v][i]!=0)&&(seen[i]==0))
if(a[v][i]+a[i][v] < min)
{
min=a[i][0]+a[v][i];
k_min=a[v][i];
nc=i;
}
}
if(min!=999)
cost+=k_min;
return nc;
}
void min_cost(int node)
{
int i,ncity;
seen[node]=1;
cout<<node+1<<"--->";
ncity=least_cost(node);
if(ncity==999)
{
ncity=0;
cout<<ncity+1;
cost+=a[node][ncity];
return;
}
min_cost(ncity);
}
int main()
{
int i,j;
cout<<"Enter the number of vertices: ";
cin>>n;
cout<<"\nEnter the Cost Matrix\n";
for(i=0;i < n;i++)
{
cout<<"\nEnter Elements of Row: "<<i+1<<"\n";
for( j=0;j < n;j++)
cin>>a[i][j];
seen[i]=0;
}
cout<<"\n\nThe cost Matrix is:";
for( i=0;i < n;i++)
{
cout<<"\n";
for(j=0;j < n;j++)
cout<<"\t"<<a[i][j];
}
cout<<"\n\nThe Path is:\n";
min_cost(0); //0-based index
cout<<"\n\nMinimum cost is "<< cost;
return 0;