Submission #922927

#TimeUsernameProblemLanguageResultExecution timeMemory
922927a_l_i_r_e_z_aMuseum (CEOI17_museum)C++17
100 / 100
302 ms171344 KiB
// 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...