Skip to content

Commit 2deea18

Browse files
authored
[20250704] BOJ / P5 / 전쟁 / 권혁준
1 parent 15be523 commit 2deea18

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static final int INF = (int)1e9 + 7;
57+
58+
static int N, M;
59+
static String[] A;
60+
static HashMap<String, Integer> deg;
61+
static HashMap<String, List<String>> V;
62+
static HashMap<String, Integer> C;
63+
static HashMap<String, Integer> S;
64+
65+
public static void main(String[] args) throws Exception {
66+
67+
io = new IOController();
68+
69+
init();
70+
solve();
71+
72+
io.close();
73+
74+
}
75+
76+
public static void init() throws Exception {
77+
78+
N = io.nextInt();
79+
M = io.nextInt();
80+
A = new String[N];
81+
deg = new HashMap<>();
82+
V = new HashMap<>();
83+
C = new HashMap<>();
84+
S = new HashMap<>();
85+
for(int i=0;i<N;i++) {
86+
String line = io.nextLine();
87+
String[] arr = line.split(" ");
88+
String a = arr[0];
89+
int c = Integer.parseInt(arr[1]);
90+
C.put(a,c);
91+
A[i] = a;
92+
V.put(a, new ArrayList<>());
93+
if(arr.length >= 3) {
94+
for(int k=2;k<arr.length;k++) {
95+
String b = arr[k];
96+
V.get(a).add(b);
97+
deg.put(b, 1);
98+
}
99+
}
100+
}
101+
102+
}
103+
104+
static void solve() throws Exception {
105+
106+
int[] dp = new int[N+1];
107+
Arrays.fill(dp, INF);
108+
dp[0] = 0;
109+
for(int i=0;i<N;i++) if(deg.getOrDefault(A[i], 0) == 0) {
110+
int[] res = dfs(A[i]);
111+
int[] ndp = new int[N+1];
112+
for(int k=0;k<=N;k++) ndp[k] = dp[k];
113+
for(int k=N;k>0;k--) if(res[k] != INF) {
114+
for(int j=N;j>=k;j--) if(dp[j-k] != INF) ndp[j] = Math.min(ndp[j], dp[j-k] + res[k]);
115+
}
116+
for(int k=0;k<=N;k++) dp[k] = ndp[k];
117+
}
118+
119+
int ans = INF;
120+
for(int i=M;i<=N;i++) ans = Math.min(ans, dp[i]);
121+
io.write(ans + "\n");
122+
123+
}
124+
125+
static int[] dfs(String n) {
126+
int child = 1;
127+
int[] dp = new int[N+1];
128+
Arrays.fill(dp, INF);
129+
dp[0] = 0;
130+
for(String i : V.getOrDefault(n, new ArrayList<>())) {
131+
int[] res = dfs(i);
132+
child += S.get(i);
133+
int[] ndp = new int[N+1];
134+
for(int k=0;k<=N;k++) ndp[k] = dp[k];
135+
for(int k=N;k>0;k--) if(res[k] != INF) {
136+
for(int j=N;j>=k;j--) if(dp[j-k] != INF) ndp[j] = Math.min(ndp[j], dp[j-k] + res[k]);
137+
}
138+
for(int k=0;k<=N;k++) dp[k] = ndp[k];
139+
}
140+
dp[child] = Math.min(dp[child], C.get(n));
141+
142+
S.put(n, child);
143+
return dp;
144+
}
145+
146+
}
147+
```

0 commit comments

Comments
 (0)