// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 10000 + 5;
const int inf = 1e9 + 7;
int n, k, r, dp[maxn][maxn][2], tmp[maxn][2], cnt[maxn];
vector<pii> adj[maxn];
void dfs(int v, int p){
cnt[v] = 1;
for(auto [u, w]: adj[v]){
if(u == p) continue;
dfs(u, v);
for(int i = 0; i <= cnt[v] + cnt[u]; i++) tmp[i][0] = tmp[i][1] = inf;
for(int i = 1; i <= cnt[v]; i++){
for(int j = 0; j <= cnt[u]; j++){
if(j == 0){
smin(tmp[i][1], dp[v][i][1]);
smin(tmp[i][0], dp[v][i][0]);
}
else{
smin(tmp[i + j][1], dp[v][i][1] + dp[u][j][1] + 2 * w);
int x = min(dp[v][i][1] + w + dp[u][j][0], dp[v][i][0] + 2 * w + dp[u][j][1]);
smin(tmp[i + j][0], x);
}
}
}
cnt[v] += cnt[u];
for(int i = 1; i <= cnt[v]; i++){
dp[v][i][0] = tmp[i][0];
dp[v][i][1] = tmp[i][1];
smin(dp[v][i][0], dp[v][i][1]);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> r;
for(int i = 0; i < n - 1; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
dfs(r, -1);
cout << dp[r][k][0] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
30548 KB |
Output is correct |
2 |
Correct |
116 ms |
31056 KB |
Output is correct |
3 |
Correct |
212 ms |
166740 KB |
Output is correct |
4 |
Correct |
146 ms |
75092 KB |
Output is correct |
5 |
Correct |
125 ms |
42324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
121 ms |
30548 KB |
Output is correct |
2 |
Correct |
116 ms |
31056 KB |
Output is correct |
3 |
Correct |
212 ms |
166740 KB |
Output is correct |
4 |
Correct |
146 ms |
75092 KB |
Output is correct |
5 |
Correct |
125 ms |
42324 KB |
Output is correct |
6 |
Correct |
127 ms |
25428 KB |
Output is correct |
7 |
Correct |
176 ms |
109624 KB |
Output is correct |
8 |
Correct |
302 ms |
4952 KB |
Output is correct |
9 |
Correct |
226 ms |
4820 KB |
Output is correct |
10 |
Correct |
122 ms |
5972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
121 ms |
30548 KB |
Output is correct |
7 |
Correct |
116 ms |
31056 KB |
Output is correct |
8 |
Correct |
212 ms |
166740 KB |
Output is correct |
9 |
Correct |
146 ms |
75092 KB |
Output is correct |
10 |
Correct |
125 ms |
42324 KB |
Output is correct |
11 |
Correct |
127 ms |
25428 KB |
Output is correct |
12 |
Correct |
176 ms |
109624 KB |
Output is correct |
13 |
Correct |
302 ms |
4952 KB |
Output is correct |
14 |
Correct |
226 ms |
4820 KB |
Output is correct |
15 |
Correct |
122 ms |
5972 KB |
Output is correct |
16 |
Correct |
117 ms |
31776 KB |
Output is correct |
17 |
Correct |
116 ms |
31312 KB |
Output is correct |
18 |
Correct |
152 ms |
84560 KB |
Output is correct |
19 |
Correct |
248 ms |
4788 KB |
Output is correct |
20 |
Correct |
117 ms |
7280 KB |
Output is correct |
21 |
Correct |
170 ms |
107604 KB |
Output is correct |
22 |
Correct |
117 ms |
31036 KB |
Output is correct |
23 |
Correct |
240 ms |
4952 KB |
Output is correct |
24 |
Correct |
124 ms |
6736 KB |
Output is correct |
25 |
Correct |
213 ms |
171344 KB |
Output is correct |