Repository navigation
Expand file tree
/
Copy pathTSP_2.java
More file actions
76 lines (59 loc) · 1.52 KB
/
Copy pathTSP_2.java
File metadata and controls
76 lines (59 loc) · 1.52 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
package bruteforce;
import java.util.*;
public class TSP_2 {
public static boolean next_permutation(int[] a) {
int i = a.length-1;
while(i>0 && a[i]<=a[i-1]) {
i--;
}
if(i <=0) return false;
int j = a.length-1;
while(a[j]<=a[i-1]){
j--;
}
int temp = a[i-1];
a[i-1] = a[j];
a[j] = temp;
i = a.length-1;
while(i<j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
return true;
}
public static void main(String[] args) {
//#10971번_외판원 순회2
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int w [][] = new int[n][n];
for(int i=0; i<w.length; i++)
for(int j=0; j<w.length; j++)
w[i][j] = sc.nextInt();
int [] d = new int[n];
for(int i=0; i<n; i++) {
d[i] = i;
}
int ans = Integer.MAX_VALUE;
do {
boolean ok = true; // 올바른 경로인지 체크
int sum = 0;
for (int i=0; i<n-1; i++) {
if (w[d[i]][d[i+1]] == 0) {
ok = false;
} else {
sum += w[d[i]][d[i+1]];
}
}
if (ok && w[d[n-1]][d[0]] != 0) {
sum+=w[d[n-1]][d[0]];
if (ans > sum) {
ans = sum;
}
}
} while(next_permutation(d));
System.out.println(ans);
}
}