-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_tsp.cpp
More file actions
74 lines (73 loc) · 1.32 KB
/
8_tsp.cpp
File metadata and controls
74 lines (73 loc) · 1.32 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
#include<bits/stdc++.h>
#define INF 9999
using namespace std;
int M[4][4]={{0,1,2,3},
{2,0,3,4},
{3,4,0,5},
{4,5,6,0}};
int q[4];
int cost=0;
int n=4;
int status[4];
void func(int a[4][4]);
int main()
{
cout<<"Given cost matrix:\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<M[i][j]<<" ";
}
cout<<"\n";
}
for(int i=0;i<n;i++)
{
status[i]=0;
}
q[0]=0;
func(M);
return 0;
}
void func(int M[4][4])
{
int next=1;
int i, j, min;
status[0]=1;
i=0;
while(i<n)
{
min=INF;
for(j=0;j<n;j++)
{
if(M[i][j]!=INF && M[i][j]!=0 && status[j]==0)
{
if(M[i][j]<min) //if distance found is shorter than current distance
{
min=M[i][j];
q[next]=j;
}
}
}
if(min==INF)
{
cost=cost+M[i][0];
break;
}
else
{
cost=cost+min;
status[q[next]]=1;
i=q[next];
next=next+1;
}
}
cout<<"Shortest Path: "<<endl;
for(i=0;i<n;i++)
{
cout<<q[i]<<"-->";
}
cout<<0;
cout<<"\n";
cout<<"Minimum Cost:"<<cost;
}