제출 #302389

#제출 시각아이디문제언어결과실행 시간메모리
302389HynDufMuseum (CEOI17_museum)C++17
80 / 100
3029 ms57032 KiB
#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) { szz += sz[v.F]; Rep(k, min(K, szz), 2) Rep(k1, min(k - 1, sz[v.F]), 1) { 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); } } } 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...