File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int N ;
7+ static int [][] cost;
8+ static int [][] dp;
9+ static final int INF = 987654321 ;
10+
11+ public static void main (String [] args ) throws IOException {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
13+
14+ N = Integer . parseInt(br. readLine());
15+ cost = new int [N ][N ];
16+
17+ for (int i = 0 ; i < N ; i++ ) {
18+ StringTokenizer st = new StringTokenizer (br. readLine());
19+ for (int j = 0 ; j < N ; j++ ) {
20+ cost[i][j] = Integer . parseInt(st. nextToken());
21+ }
22+ }
23+
24+ dp = new int [N ][1 << N ];
25+ for (int i = 0 ; i < N ; i++ ) {
26+ Arrays . fill(dp[i], - 1 );
27+ }
28+
29+ System . out. println(solve(0 , 0 ));
30+ }
31+
32+ static int solve (int person , int mask ) {
33+ if (person == N ) {
34+ return 0 ;
35+ }
36+
37+ if (dp[person][mask] != - 1 ) {
38+ return dp[person][mask];
39+ }
40+
41+ int result = INF ;
42+
43+ for (int job = 0 ; job < N ; job++ ) {
44+ if ((mask & (1 << job)) == 0 ) {
45+ int nextMask = mask | (1 << job);
46+ result = Math . min(result, cost[person][job] + solve(person + 1 , nextMask));
47+ }
48+ }
49+
50+ return dp[person][mask] = result;
51+ }
52+ }
53+ ```
You can’t perform that action at this time.
0 commit comments