Skip to content

Commit b32e717

Browse files
authored
Merge pull request #992 from AlgorithmWithGod/khj20006
[20250927] BOJ / P2 / 인생은 B와 D 사이의 C다 / 권혁준
2 parents 3c68076 + 0fe8639 commit b32e717

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
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())
24+
nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static int N;
58+
static long B, D;
59+
static List<Integer>[] tree;
60+
static long[] s;
61+
static long[][] dp;
62+
63+
public static void main(String[] args) throws Exception {
64+
65+
io = new IOController();
66+
67+
N = io.nextInt();
68+
B = io.nextInt();
69+
D = io.nextInt();
70+
tree = new List[N+1];
71+
s = new long[N+1];
72+
dp = new long[N+1][35];
73+
for(int i=1;i<=N;i++) tree[i] = new ArrayList<>();
74+
75+
for(int i=1;i<N;i++) {
76+
int a = io.nextInt();
77+
int b = io.nextInt();
78+
tree[a].add(b);
79+
tree[b].add(a);
80+
}
81+
82+
dfs(1,0);
83+
84+
long ans = Long.MAX_VALUE;
85+
for(int i=0;i<35;i++) {
86+
ans = Math.min(ans, dp[1][i]);
87+
}
88+
io.write(ans + "\n");
89+
90+
io.close();
91+
92+
}
93+
94+
static void dfs(int n, int p) {
95+
s[n] = 1;
96+
int children = 0;
97+
for(int i:tree[n]) if(i != p) {
98+
dfs(i, n);
99+
s[n] += s[i];
100+
children++;
101+
}
102+
dp[n][0] = D*(s[n] - 1);
103+
for(int h=1;h<35;h++) {
104+
if(children == 0) dp[n][h] = B*((1L<<(h+1)) - 2);
105+
else {
106+
List<long[]> tmp = new ArrayList<>();
107+
for(int i:tree[n]) if(i != p) {
108+
tmp.add(new long[]{D*s[i] - dp[i][h-1], i});
109+
}
110+
Collections.sort(tmp, (a,b) -> Long.compare(b[0],a[0]));
111+
if(children == 1) {
112+
dp[n][h] = dp[(int)tmp.get(0)[1]][h-1] + B*((1L<<h)-1);
113+
}
114+
else {
115+
dp[n][h] = dp[(int)tmp.get(0)[1]][h-1] + dp[(int)tmp.get(1)[1]][h-1];
116+
for(int i=2;i<tmp.size();i++) dp[n][h] += D*s[(int)tmp.get(i)[1]];
117+
}
118+
}
119+
dp[n][h] = Math.min(dp[n][h], D*(s[n]-1) + B*((1L<<(h+1))-2));
120+
}
121+
}
122+
123+
}
124+
```

0 commit comments

Comments
 (0)