// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e4+5;
int n, k, r, cnt[maxn], dp[maxn][maxn][2], tmp[maxn][2];
vector <pii> adj[maxn];
void dfs(int v = r, int p = r) {
cnt[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (u == p) continue;
dfs(u, v);
for (int i = 1; i <= cnt[v]; i++) {
smin(tmp[i][0], dp[v][i][0]);
smin(tmp[i][1], dp[v][i][1]);
for (int j = 1; j <= cnt[u]; j++) {
smin(tmp[i+j][0], dp[v][i][0]+dp[u][j][0]+e.S*2);
smin(tmp[i+j][1],
min(dp[v][i][0]+dp[u][j][1]+e.S, dp[v][i][1]+dp[u][j][0]+e.S*2));
}
}
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];
tmp[i][0] = tmp[i][1] = inf;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(tmp, 63, sizeof tmp);
cin >> n >> k >> r;
r--;
for (int i = 1; i < n; i++) {
int v, u, w; cin >> v >> u >> w;
adj[--v].pb(mp(--u, w));
adj[u].pb(mp(v, w));
}
dfs();
cout << dp[r][k][1] << '\n';
// for (int i = 1; i <= n; i++) cout << dp[r][i][1] << '\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 |
124 ms |
31200 KB |
Output is correct |
2 |
Correct |
115 ms |
31212 KB |
Output is correct |
3 |
Correct |
213 ms |
166880 KB |
Output is correct |
4 |
Correct |
144 ms |
74984 KB |
Output is correct |
5 |
Correct |
123 ms |
42324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
31200 KB |
Output is correct |
2 |
Correct |
115 ms |
31212 KB |
Output is correct |
3 |
Correct |
213 ms |
166880 KB |
Output is correct |
4 |
Correct |
144 ms |
74984 KB |
Output is correct |
5 |
Correct |
123 ms |
42324 KB |
Output is correct |
6 |
Correct |
113 ms |
25680 KB |
Output is correct |
7 |
Correct |
171 ms |
109712 KB |
Output is correct |
8 |
Correct |
244 ms |
5100 KB |
Output is correct |
9 |
Correct |
190 ms |
4940 KB |
Output is correct |
10 |
Correct |
120 ms |
5948 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 |
124 ms |
31200 KB |
Output is correct |
7 |
Correct |
115 ms |
31212 KB |
Output is correct |
8 |
Correct |
213 ms |
166880 KB |
Output is correct |
9 |
Correct |
144 ms |
74984 KB |
Output is correct |
10 |
Correct |
123 ms |
42324 KB |
Output is correct |
11 |
Correct |
113 ms |
25680 KB |
Output is correct |
12 |
Correct |
171 ms |
109712 KB |
Output is correct |
13 |
Correct |
244 ms |
5100 KB |
Output is correct |
14 |
Correct |
190 ms |
4940 KB |
Output is correct |
15 |
Correct |
120 ms |
5948 KB |
Output is correct |
16 |
Correct |
116 ms |
32080 KB |
Output is correct |
17 |
Correct |
115 ms |
31324 KB |
Output is correct |
18 |
Correct |
151 ms |
84716 KB |
Output is correct |
19 |
Correct |
207 ms |
5144 KB |
Output is correct |
20 |
Correct |
131 ms |
6936 KB |
Output is correct |
21 |
Correct |
167 ms |
107860 KB |
Output is correct |
22 |
Correct |
116 ms |
31056 KB |
Output is correct |
23 |
Correct |
202 ms |
4948 KB |
Output is correct |
24 |
Correct |
107 ms |
6740 KB |
Output is correct |
25 |
Correct |
212 ms |
171908 KB |
Output is correct |