[Link] https://www.acmicpc.net/problem/1311
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
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader br;
static int n = 0;
static int[][] cost;
static int[][] dp;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int[] arr;
n = toi(br.readLine());
cost = new int[n][n];
dp = new int[n][(1 << n) - 1];
for(int i = 0; i < n; i++) cost[i] = getArr();
print(dfs(0, 0));
}
static int dfs(int personIdx, int bits) {
if(personIdx == n) return 0;
if(dp[personIdx][bits] != 0) return dp[personIdx][bits];
int min = 300000;
for(int taskIdx = 0; taskIdx < n; taskIdx++) {
if((bits & 1 << taskIdx) != 0) continue;
else min = Math.min(min, cost[personIdx][taskIdx] + dfs(personIdx + 1, bits | 1 << taskIdx));
}
return dp[personIdx][bits] = min;
}
static int toi(String s) { return Integer.parseInt(s); }
static String[] getLine() throws IOException { return br.readLine().split(" "); }
static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
static <T> void print(T s) { System.out.print(s); }
static <T> void println(T s) { System.out.println(s); }
}