-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravelling_salesman_greedy.java
61 lines (61 loc) · 1.12 KB
/
travelling_salesman_greedy.java
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
import java.io.*;
import java.util.*;
class prg
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[][]=new int[n][n];
int visit[]=new int[n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
visit[i]=0;
}
int cost=0;
int counter=1;
int node=0;
int path[]=new int[n+1];
path[0]=1;
int t=1;
while(counter < n)
{
int jj=0;
int min=999;
int k=node;
for(int j=0;j<n;j++)
{
if(arr[node][j]!=0)
{
if(visit[j]==0)
{
if(arr[node][j]<min)
{
min=arr[node][j];
jj=j;
}
}
}
}
System.out.println("min= "+ min);
visit[node]=1;
node=jj;
arr[node][k]=0;
cost=cost+min;
counter++;
path[t]=node+1;
t++;
}
cost=cost+arr[0][node];
path[n]=1;
for(int i=0;i<=n;i++)
{
System.out.print(path[i] + " ");
}
System.out.print("cost="+ cost);
}
}