# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1252180 | rayan_bd | Museum (CEOI17_museum) | C++20 | 526 ms | 1114112 KiB |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
#define int long long
const int INF = 5e18;
vector<pair<int, int>> adj[MAXN];
int dp[MAXN][MAXN][2], sz[MAXN];
int n, k, x;
void dfs(int u, int par) {
dp[u][1][0] = dp[u][1][1] = 0;
sz[u] = 1;
for (auto [v, cost] : adj[u]) {
if (v == par) continue;
dfs(v, u);
for(int i = sz[u]; i >= 0; --i){
for(int j = sz[v]; j >= 0; --j){
dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][0] + dp[v][j][0] + cost * 2);
dp[u][i + j][1] = min(dp[u][i + j][1], dp[u][i][1] + dp[v][j][0] + cost * 2);
dp[u][i + j][1] = min(dp[u][i + j][1], dp[u][i][0] + dp[v][j][1] + cost);
}
}
sz[u] += sz[v];
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> k >> x;
for (int i = 1; i < n; ++i) {
int a, b, c;
cin >> a >> b >> c;
adj[a].emplace_back(b, c);
adj[b].emplace_back(a, c);
}
memset(dp, 0x3f, sizeof(dp));
dfs(x, -1);
int answer = min(dp[x][k][0], dp[x][k][1]);
cout << dp[x][k][1] << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |