// Author: _Sherbiny
#include "bits/stdc++.h"
using namespace std;
#ifdef DBG
#include "./debug.h"
#else
#define dbg(...)
#endif
typedef long long ll;
#define endl '\n'
#define int ll
//==================//
const int N = 1e4 + 2;
int n, k;
array<int, 2> oo;
vector<array<int, 2>> dp[N];
vector<vector<pair<int, int>>> adj;
void merge(int u, int v, int c) {
auto ret = dp[u];
ret.resize(dp[u].size() + dp[v].size() - 1, oo);
for(int i = 1; i < dp[u].size(); ++i)
for(int j = 1; j < dp[v].size(); ++j) {
ret[i + j][0] = min(ret[i + j][0], dp[u][i][0] + dp[v][j][0] + c * 2);
ret[i + j][1] = min({ret[i + j][1], dp[u][i][1] + dp[v][j][0] + c * 2, dp[u][i][0] + dp[v][j][1] + c});
}
swap(ret, dp[u]);
}
void go(int u, int p = 0) {
for(auto[v, c]: adj[u])
if(v != p) go(v, u);
dp[u].assign(2, {});
for(auto &[v, c]: adj[u])
if(v != p) merge(u, v, c);
}
void magic() {
cin >> n >> k;
int x; cin >> x;
oo.fill(2e18);
adj.assign(n + 1, {});
for (int i = 0; i < n - 1; ++i) {
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
go(x);
cout << dp[x][k][1];
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
while (t--) magic();
}
Compilation message
museum.cpp: In function 'void merge(ll, ll, ll)':
museum.cpp:27:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 1; i < dp[u].size(); ++i)
| ~~^~~~~~~~~~~~~~
museum.cpp:28:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int j = 1; j < dp[v].size(); ++j) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
5960 KB |
Output is correct |
2 |
Correct |
97 ms |
6436 KB |
Output is correct |
3 |
Correct |
342 ms |
286888 KB |
Output is correct |
4 |
Correct |
179 ms |
96728 KB |
Output is correct |
5 |
Correct |
113 ms |
24732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
5960 KB |
Output is correct |
2 |
Correct |
97 ms |
6436 KB |
Output is correct |
3 |
Correct |
342 ms |
286888 KB |
Output is correct |
4 |
Correct |
179 ms |
96728 KB |
Output is correct |
5 |
Correct |
113 ms |
24732 KB |
Output is correct |
6 |
Correct |
98 ms |
5024 KB |
Output is correct |
7 |
Correct |
234 ms |
188132 KB |
Output is correct |
8 |
Correct |
269 ms |
2964 KB |
Output is correct |
9 |
Correct |
174 ms |
3552 KB |
Output is correct |
10 |
Correct |
110 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
95 ms |
5960 KB |
Output is correct |
7 |
Correct |
97 ms |
6436 KB |
Output is correct |
8 |
Correct |
342 ms |
286888 KB |
Output is correct |
9 |
Correct |
179 ms |
96728 KB |
Output is correct |
10 |
Correct |
113 ms |
24732 KB |
Output is correct |
11 |
Correct |
98 ms |
5024 KB |
Output is correct |
12 |
Correct |
234 ms |
188132 KB |
Output is correct |
13 |
Correct |
269 ms |
2964 KB |
Output is correct |
14 |
Correct |
174 ms |
3552 KB |
Output is correct |
15 |
Correct |
110 ms |
5464 KB |
Output is correct |
16 |
Correct |
98 ms |
7116 KB |
Output is correct |
17 |
Correct |
101 ms |
6684 KB |
Output is correct |
18 |
Correct |
186 ms |
111824 KB |
Output is correct |
19 |
Correct |
167 ms |
4288 KB |
Output is correct |
20 |
Correct |
97 ms |
6256 KB |
Output is correct |
21 |
Correct |
228 ms |
170412 KB |
Output is correct |
22 |
Correct |
98 ms |
6368 KB |
Output is correct |
23 |
Correct |
167 ms |
2944 KB |
Output is correct |
24 |
Correct |
116 ms |
5464 KB |
Output is correct |
25 |
Correct |
343 ms |
289748 KB |
Output is correct |