#include <bits/stdc++.h>
using namespace std;
template<typename T> bool minimize(T& a, const T& b){
if(a > b) return a = b, true; return false;
}
#define sizeVec(v) (int)v.size()
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
#endif // LOCAL
int N, K, X;
cin >> N >> K >> X; --X;
vector<vector<pair<int, int>>> adj(N);
for(int i = 1; i < N; ++i){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
const int inf = 1e9;
vector<int> sz(N);
vector<vector<int>> f(N), g(N);
function<void(int, int)> dfs = [&](int u, int p){
sz[u] = 1;
for(auto [v, w] : adj[u]) if(v != p){
dfs(v, u);
sz[u] += sz[v];
}
f[u].resize(sz[u] + 1, inf);
g[u].resize(sz[u] + 1, inf);
f[u][1] = 0; //at somewhere
g[u][1] = 0; //at u
int sumSize = 1;
for(auto [v, w] : adj[u]) if(v != p){
for(int i = sumSize; i >= 1; --i){
for(int j = sz[v]; j >= 1; --j){
minimize(f[u][i + j], min(f[u][i] + g[v][j] + 2 * w, g[u][i] + f[v][j] + w));
minimize(g[u][i + j], g[u][i] + g[v][j] + 2 * w);
}
}
sumSize += sz[v];
}
};
dfs(X, -1);
cout << min(f[X][K], g[X][K]) << '\n';
return 0;
}
Compilation message
museum.cpp: In function 'bool minimize(T&, const T&)':
museum.cpp:6:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
6 | if(a > b) return a = b, true; return false;
| ^~
museum.cpp:6:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
6 | if(a > b) return a = b, true; return false;
| ^~~~~~
museum.cpp: In lambda function:
museum.cpp:37:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
37 | for(auto [v, w] : adj[u]) if(v != p){
| ^
museum.cpp:48:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | for(auto [v, w] : adj[u]) if(v != p){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
3656 KB |
Output is correct |
2 |
Correct |
79 ms |
4168 KB |
Output is correct |
3 |
Correct |
189 ms |
136776 KB |
Output is correct |
4 |
Correct |
130 ms |
44780 KB |
Output is correct |
5 |
Correct |
97 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
3656 KB |
Output is correct |
2 |
Correct |
79 ms |
4168 KB |
Output is correct |
3 |
Correct |
189 ms |
136776 KB |
Output is correct |
4 |
Correct |
130 ms |
44780 KB |
Output is correct |
5 |
Correct |
97 ms |
12628 KB |
Output is correct |
6 |
Correct |
100 ms |
3144 KB |
Output is correct |
7 |
Correct |
140 ms |
80636 KB |
Output is correct |
8 |
Correct |
97 ms |
2384 KB |
Output is correct |
9 |
Correct |
89 ms |
2632 KB |
Output is correct |
10 |
Correct |
84 ms |
3188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
105 ms |
3656 KB |
Output is correct |
7 |
Correct |
79 ms |
4168 KB |
Output is correct |
8 |
Correct |
189 ms |
136776 KB |
Output is correct |
9 |
Correct |
130 ms |
44780 KB |
Output is correct |
10 |
Correct |
97 ms |
12628 KB |
Output is correct |
11 |
Correct |
100 ms |
3144 KB |
Output is correct |
12 |
Correct |
140 ms |
80636 KB |
Output is correct |
13 |
Correct |
97 ms |
2384 KB |
Output is correct |
14 |
Correct |
89 ms |
2632 KB |
Output is correct |
15 |
Correct |
84 ms |
3188 KB |
Output is correct |
16 |
Correct |
79 ms |
4424 KB |
Output is correct |
17 |
Correct |
93 ms |
4168 KB |
Output is correct |
18 |
Correct |
123 ms |
54600 KB |
Output is correct |
19 |
Correct |
100 ms |
2528 KB |
Output is correct |
20 |
Correct |
79 ms |
3400 KB |
Output is correct |
21 |
Correct |
138 ms |
77604 KB |
Output is correct |
22 |
Correct |
81 ms |
3968 KB |
Output is correct |
23 |
Correct |
93 ms |
2392 KB |
Output is correct |
24 |
Correct |
76 ms |
3156 KB |
Output is correct |
25 |
Correct |
182 ms |
140920 KB |
Output is correct |