#include <bits/stdc++.h>
#define task "M"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e4 + 1, INF = 1e9;
int n, K, r, dp[N][N][2], sz[N];
vii g[N];
void upd(int &x, int y) {x = min(x, y);}
void dfs(int u, int p)
{
for (ii v : g[u]) if (v.F != p)
{
dfs(v.F, u);
sz[u] += sz[v.F];
}
sz[u]++;
int szz = 1;
rep(i, 0, 1) rep(j, 0, min(K, sz[u])) dp[u][j][i] = INF;
dp[u][1][0] = dp[u][1][1] = 0;
for (ii v : g[u]) if (v.F != p)
{
Rep(k, min(K, szz + sz[v.F]), 2) Rep(k1, min(k - 1, sz[v.F]), max(1, k - szz))
{
upd(dp[u][k][0], dp[u][k - k1][0] + dp[v.F][k1][0] + (v.S << 1));
upd(dp[u][k][1], dp[u][k - k1][1] + dp[v.F][k1][0] + (v.S << 1));
upd(dp[u][k][1], dp[u][k - k1][0] + dp[v.F][k1][1] + v.S);
}
szz += sz[v.F];
}
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n >> K >> r;
rep(i, 2, n)
{
int u, v, w;
cin >> u >> v >> w;
g[u].eb(v, w);
g[v].eb(u, w);
}
dfs(r, 0);
cout << dp[r][K][1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
43128 KB |
Output is correct |
2 |
Correct |
38 ms |
43128 KB |
Output is correct |
3 |
Correct |
40 ms |
45688 KB |
Output is correct |
4 |
Correct |
34 ms |
44544 KB |
Output is correct |
5 |
Correct |
35 ms |
43904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
43128 KB |
Output is correct |
2 |
Correct |
38 ms |
43128 KB |
Output is correct |
3 |
Correct |
40 ms |
45688 KB |
Output is correct |
4 |
Correct |
34 ms |
44544 KB |
Output is correct |
5 |
Correct |
35 ms |
43904 KB |
Output is correct |
6 |
Correct |
32 ms |
42880 KB |
Output is correct |
7 |
Correct |
37 ms |
45440 KB |
Output is correct |
8 |
Correct |
35 ms |
42624 KB |
Output is correct |
9 |
Correct |
33 ms |
42616 KB |
Output is correct |
10 |
Correct |
32 ms |
42728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
2 |
Correct |
1 ms |
640 KB |
Output is correct |
3 |
Correct |
1 ms |
640 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
35 ms |
43128 KB |
Output is correct |
7 |
Correct |
38 ms |
43128 KB |
Output is correct |
8 |
Correct |
40 ms |
45688 KB |
Output is correct |
9 |
Correct |
34 ms |
44544 KB |
Output is correct |
10 |
Correct |
35 ms |
43904 KB |
Output is correct |
11 |
Correct |
32 ms |
42880 KB |
Output is correct |
12 |
Correct |
37 ms |
45440 KB |
Output is correct |
13 |
Correct |
35 ms |
42624 KB |
Output is correct |
14 |
Correct |
33 ms |
42616 KB |
Output is correct |
15 |
Correct |
32 ms |
42728 KB |
Output is correct |
16 |
Correct |
56 ms |
43512 KB |
Output is correct |
17 |
Correct |
132 ms |
44280 KB |
Output is correct |
18 |
Correct |
73 ms |
56952 KB |
Output is correct |
19 |
Correct |
84 ms |
42700 KB |
Output is correct |
20 |
Correct |
56 ms |
43128 KB |
Output is correct |
21 |
Correct |
261 ms |
117752 KB |
Output is correct |
22 |
Correct |
179 ms |
44656 KB |
Output is correct |
23 |
Correct |
279 ms |
43000 KB |
Output is correct |
24 |
Correct |
174 ms |
43756 KB |
Output is correct |
25 |
Correct |
341 ms |
181084 KB |
Output is correct |