Skip to content

Commit 01136c4

Browse files
authored
Merge pull request #379 from AlgorithmWithGod/lkhyun
[20250622] BOJ / G1 / 외판원 순회 / 이강현
2 parents 0196813 + 1a5b97b commit 01136c4

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N;
10+
static int[][] cost;
11+
static int[][] dp;
12+
13+
public static void main(String[] args) throws IOException {
14+
N = Integer.parseInt(br.readLine());
15+
cost = new int[N][N];
16+
dp = new int[1 << N][N];
17+
18+
for (int i = 0; i < N; i++) {
19+
st = new StringTokenizer(br.readLine());
20+
for (int j = 0; j < N; j++) {
21+
cost[i][j] = Integer.parseInt(st.nextToken());
22+
}
23+
}
24+
25+
for (int i = 0; i < (1 << N); i++) {
26+
Arrays.fill(dp[i], Integer.MAX_VALUE);
27+
}
28+
29+
dp[1][0] = 0;
30+
31+
for (int i = 1; i < (1 << N); i++) {
32+
for (int j = 0; j < N; j++) {
33+
if ((i & (1 << j)) == 0) continue;
34+
if (dp[i][j] == Integer.MAX_VALUE) continue;
35+
36+
for (int k = 0; k < N; k++) {
37+
if ((i & (1 << k)) != 0) continue;
38+
if (cost[j][k] == 0) continue;
39+
40+
dp[i | (1 << k)][k] = Math.min(dp[i | (1 << k)][k], dp[i][j] + cost[j][k]);
41+
}
42+
}
43+
}
44+
45+
int answer = Integer.MAX_VALUE;
46+
47+
for (int i = 1; i < N; i++) {
48+
if (dp[(1 << N) - 1][i] != Integer.MAX_VALUE && cost[i][0] != 0) {
49+
answer = Math.min(answer, dp[(1 << N) - 1][i] + cost[i][0]);
50+
}
51+
}
52+
53+
bw.write(answer + "");
54+
bw.close();
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)