#include <bits/stdc++.h>
#define I64 long long
using namespace std;
const int N = 10000;
const I64 A = numeric_limits<I64>::max();
vector<I64> operator + (vector<I64> a, vector<I64> b) {
int n = (int) a.size(), m = (int) b.size();
vector<I64> c(n + m - 1, A);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] < A && b[j] < A) {
c[i + j] = min(c[i + j], a[i] + b[j]);
}
}
}
return c;
}
vector<I64> min(vector<I64> a, vector<I64> b) {
int n = (int) a.size();
vector<I64> c(n);
for (int i = 0; i < n; i++) {
c[i] = min(a[i], b[i]);
}
return c;
}
int n, k, S;
vector<pair<int, int>> e[N];
int p[N];
vector<I64> f[N][2];
void DFS(int u) {
f[u][0] = {0, 0};
f[u][1] = {0, 0};
for (auto [v, x] : e[u]) {
if (v != p[u]) {
p[v] = u;
DFS(v);
vector<I64> g[2];
g[0] = f[v][0];
g[1] = f[v][1];
// for (auto &i : g[0]) {
// if (i < A) {
// i = i + x;
// }
// }
// for (auto &i : g[1]) {
// if (i < A) {
// i = i + x * 2;
// }
// }
for (int i = 1; i < (int) g[0].size(); i++) {
g[0][i] = g[0][i] + x;
}
for (int i = 1; i < (int) g[1].size(); i++) {
g[1][i] = g[1][i] + x * 2;
}
f[u][0] = min(f[u][0] + g[1], f[u][1] + g[0]);
f[u][1] = f[u][1] + g[1];
}
}
}
void solve() {
cin >> n >> k >> S;
for (int i = 0; i < n - 1; i++) {
int u, v;
int x;
cin >> u >> v >> x;
u = u - 1, v = v - 1;
e[u].push_back({v, x});
e[v].push_back({u, x});
}
S = S - 1;
p[S] = 0 - 1;
DFS(S);
// for (int i = 0; i < n; i++) {
// for (auto x : f[i][0]) {
// cout << x << " ";
// }
// cout << "\n";
// for (auto x : f[i][1]) {
// cout << x << " ";
// }
// cout << "\n\n";
// }
cout << f[S][0][k] << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1104 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
2 ms |
1176 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
6684 KB |
Output is correct |
2 |
Correct |
164 ms |
7700 KB |
Output is correct |
3 |
Correct |
642 ms |
407752 KB |
Output is correct |
4 |
Correct |
317 ms |
123256 KB |
Output is correct |
5 |
Correct |
174 ms |
26528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
6684 KB |
Output is correct |
2 |
Correct |
164 ms |
7700 KB |
Output is correct |
3 |
Correct |
642 ms |
407752 KB |
Output is correct |
4 |
Correct |
317 ms |
123256 KB |
Output is correct |
5 |
Correct |
174 ms |
26528 KB |
Output is correct |
6 |
Correct |
154 ms |
5216 KB |
Output is correct |
7 |
Correct |
431 ms |
231980 KB |
Output is correct |
8 |
Correct |
673 ms |
3464 KB |
Output is correct |
9 |
Correct |
529 ms |
3760 KB |
Output is correct |
10 |
Correct |
202 ms |
5224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1104 KB |
Output is correct |
2 |
Correct |
2 ms |
1104 KB |
Output is correct |
3 |
Correct |
2 ms |
1176 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
192 ms |
6684 KB |
Output is correct |
7 |
Correct |
164 ms |
7700 KB |
Output is correct |
8 |
Correct |
642 ms |
407752 KB |
Output is correct |
9 |
Correct |
317 ms |
123256 KB |
Output is correct |
10 |
Correct |
174 ms |
26528 KB |
Output is correct |
11 |
Correct |
154 ms |
5216 KB |
Output is correct |
12 |
Correct |
431 ms |
231980 KB |
Output is correct |
13 |
Correct |
673 ms |
3464 KB |
Output is correct |
14 |
Correct |
529 ms |
3760 KB |
Output is correct |
15 |
Correct |
202 ms |
5224 KB |
Output is correct |
16 |
Correct |
148 ms |
8492 KB |
Output is correct |
17 |
Correct |
154 ms |
7932 KB |
Output is correct |
18 |
Correct |
356 ms |
150036 KB |
Output is correct |
19 |
Correct |
448 ms |
3480 KB |
Output is correct |
20 |
Correct |
175 ms |
5604 KB |
Output is correct |
21 |
Correct |
396 ms |
215448 KB |
Output is correct |
22 |
Correct |
157 ms |
7280 KB |
Output is correct |
23 |
Correct |
476 ms |
3696 KB |
Output is correct |
24 |
Correct |
194 ms |
5404 KB |
Output is correct |
25 |
Correct |
631 ms |
394188 KB |
Output is correct |