Skip to content

Commit 3e25e7f

Browse files
authored
Merge pull request #197 from AlgorithmWithGod/khj20006
[20250303] BOJ / P5 / 발전소 / 권혁준
2 parents 838cee9 + 02e7c6c commit 3e25e7f

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
7+
class Main {
8+
9+
// IO field
10+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
static StringTokenizer st;
13+
14+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
15+
static int nextInt() {return Integer.parseInt(st.nextToken());}
16+
static long nextLong() {return Long.parseLong(st.nextToken());}
17+
static void bwEnd() throws Exception {bw.flush();bw.close();}
18+
19+
// Additional field
20+
static int[][] D, C;
21+
static int N, P, ans;
22+
static final int INF = (int)1e9;
23+
static int start = 0;
24+
25+
public static void main(String[] args) throws Exception {
26+
27+
ready();
28+
solve();
29+
30+
bwEnd();
31+
32+
}
33+
34+
static void ready() throws Exception{
35+
36+
N = Integer.parseInt(br.readLine());
37+
D = new int[N][1<<N];
38+
for(int i=0;i<N;i++) Arrays.fill(D[i], INF);
39+
C = new int[N][N];
40+
for(int i=0;i<N;i++) {
41+
nextLine();
42+
for(int j=0;j<N;j++) C[i][j] = nextInt();
43+
}
44+
45+
char[] info = br.readLine().toCharArray();
46+
int temp = 0;
47+
for(int i=0;i<N;i++) {
48+
start |= (info[i] == 'Y' ? (1<<i) : 0);
49+
temp += (info[i] == 'Y' ? 1 : 0);
50+
}
51+
52+
P = Integer.parseInt(br.readLine());
53+
for(int i=0;i<N;i++) if((1<<i) != 0) D[i][start] = 0;
54+
ans = temp >= P ? 0 : INF;
55+
56+
}
57+
58+
static void solve() throws Exception{
59+
60+
for(int i=0;i<N;i++) dp(i,(1<<N)-1);
61+
bw.write((ans==INF ? -1 : ans) + "\n");
62+
63+
}
64+
65+
static int dp(int n, int k) throws Exception {
66+
if(D[n][k] != INF || (start & (1<<n)) != 0) return D[n][k];
67+
int p = k^(1<<n);
68+
int prev = INF, cost = INF, cnt = 0;
69+
for(int i=0;i<N;i++){
70+
if((p & (1<<i)) == 0) continue;
71+
cnt++;
72+
prev = Math.min(prev, dp(i, p));
73+
cost = Math.min(cost, C[i][n]);
74+
}
75+
D[n][k] = Math.min(D[n][k], prev+cost);
76+
if(cnt >= P-1) ans = Math.min(ans, D[n][k]);
77+
return D[n][k];
78+
}
79+
80+
}
81+
82+
```

0 commit comments

Comments
 (0)