File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed
Expand file tree Collapse file tree 1 file changed +75
-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+
7+ static int [][] arr;
8+ static int [] dp;
9+ static int N ;
10+ static int P ;
11+ static final int DEFAULT = Integer . MAX_VALUE >> 1 ;
12+ static int min = DEFAULT ;
13+
14+ public static void main (String [] args ) throws Exception {
15+ N = read();
16+
17+ arr = new int [N ][N ];
18+ dp = new int [(1 << N )];
19+
20+ Arrays . fill(dp, DEFAULT );
21+
22+ int start = 0 ;
23+ for (int i = 0 ; i < N ; i++ ) {
24+ for (int j = 0 ; j < N ; j++ ) {
25+ arr[i][j] = read();
26+ }
27+ }
28+ for (int i = 0 ; i < N ; i++ ) {
29+ if (System . in. read() == ' Y' ) {
30+ start |= (1 << i);
31+ }
32+ }
33+ System . in. read();
34+ P = read();
35+ dp[start] = 0 ;
36+
37+ for (int i = start; i < (1 << N ); i++ ) {
38+
39+ // 갯수 세기
40+ int cnt = 0 ;
41+
42+ // 들어가는 거
43+ for (int j = 0 ; j < N ; j++ ) {
44+ // 나가는 거
45+ if (((1 << j) | i) != i) continue ;
46+ cnt++ ;
47+ for (int a = 0 ; a < N ; a++ ) {
48+ int ll = ((1 << a) | i);
49+ if (ll == i) continue ;
50+ dp[ll] = Math . min(dp[ll], dp[i] + arr[j][a]);
51+ }
52+ }
53+
54+ if (cnt >= P ) min = Math . min(dp[i], min);
55+
56+ }
57+
58+ System . out. println(min == DEFAULT ? - 1 : min);
59+ }
60+
61+
62+ private static int read () throws Exception {
63+ int d, o;
64+ d = System . in. read();
65+
66+ o = d & 15 ;
67+
68+ while ((d = System . in. read()) > 32 )
69+ o = (o << 3 ) + (o << 1 ) + (d & 15 );
70+
71+ return o;
72+ }
73+
74+ }
75+ ```
You can’t perform that action at this time.
0 commit comments