Submission #1252162

#TimeUsernameProblemLanguageResultExecution timeMemory
1252162rayan_bdMuseum (CEOI17_museum)C++20
0 / 100
2 ms580 KiB
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MAXN = 505; const int MAXK = 505; vector<pair<int, int>> adj[MAXN]; int dp[MAXN][MAXK][2]; int n, k, x; void dfs(int u, int par) { dp[u][1][0] = 0; dp[u][1][1] = 0; for (auto [v, cost] : adj[u]) { if (v == par) continue; dfs(v, u); int tmp[MAXK][2]; for (int i = 0; i <= k; ++i) tmp[i][0] = tmp[i][1] = INF; for (int i = 1; i <= k; ++i) { if (dp[u][i][0] == INF && dp[u][i][1] == INF) continue; for (int j = 1; j <= k - i + 1; ++j) { if (dp[v][j][0] == INF && dp[v][j][1] == INF) continue; tmp[i + j - 1][0] = min(tmp[i + j - 1][0], dp[u][i][0] + dp[v][j][0] + 2 * cost); tmp[i + j - 1][1] = min(tmp[i + j - 1][1], dp[u][i][0] + dp[v][j][1] + cost); tmp[i + j - 1][1] = min(tmp[i + j - 1][1], dp[u][i][1] + dp[v][j][0] + 2 * cost); } } for (int i = 1; i <= k; ++i) { dp[u][i][0] = min(dp[u][i][0], tmp[i][0]); dp[u][i][1] = min(dp[u][i][1], tmp[i][1]); } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> x >> k; 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); } for (int i = 1; i <= n; ++i) for (int j = 0; j <= k; ++j) dp[i][j][0] = dp[i][j][1] = INF; dfs(x, -1); int answer = min(dp[x][k][0], dp[x][k][1]); cout << answer << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...