https://www.acmicpc.net/problem/10971
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int[][] W;
static boolean[] visited;
static int ans = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
W = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
W[i][j] = Integer.parseInt(st.nextToken());
}
}
for(int i = 0; i < N; i++){
visited = new boolean[N];
visited[i] = true;
dfs(i, i, 1, 0);
}
System.out.println(ans);
}
static void dfs(int start, int cur, int cnt, int cost) {
if (cnt == N) {
if (W[cur][start] != 0) {
ans = Math.min(ans, cost + W[cur][start]);
}
return;
}
for (int i = 0; i < N; i++) {
if (!visited[i] && W[cur][i] != 0) {
visited[i] = true;
dfs(start, i, cnt + 1, cost + W[cur][i]);
visited[i] = false;
}
}
}
}
if(cnt == N){
ans = Math.min(ans, cost + W[cur][start]);
return;
}
if (cnt == N) {
if(W[cur][start] != 0){
ans = Math.min(ans, cost + W[cur][start]);
}
return;
}
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int[] row : map) {
System.out.println(Arrays.toString(row));
}
Arrays.stream(map)
.map(row -> Arrays.toString(row))
.forEach(System.out::println);