제출 #925275

#제출 시각아이디문제언어결과실행 시간메모리
925275Amirreza_FakhriMuseum (CEOI17_museum)C++17
100 / 100
244 ms171908 KiB
// In the name of the God #include <bits/stdc++.h> #define ll long long // #define int long long #define pb push_back #define F first #define S second #define mp make_pair #define pii pair <int, int> #define smin(x, y) (x) = min((x), (y)) #define smax(x, y) (x) = max((x), (y)) #define all(x) (x).begin(), (x).end() using namespace std; const int inf = 1e9+7; const int mod = 998244353; const int maxn = 1e4+5; int n, k, r, cnt[maxn], dp[maxn][maxn][2], tmp[maxn][2]; vector <pii> adj[maxn]; void dfs(int v = r, int p = r) { cnt[v] = 1; for (pii e : adj[v]) { int u = e.F; if (u == p) continue; dfs(u, v); for (int i = 1; i <= cnt[v]; i++) { smin(tmp[i][0], dp[v][i][0]); smin(tmp[i][1], dp[v][i][1]); for (int j = 1; j <= cnt[u]; j++) { smin(tmp[i+j][0], dp[v][i][0]+dp[u][j][0]+e.S*2); smin(tmp[i+j][1], min(dp[v][i][0]+dp[u][j][1]+e.S, dp[v][i][1]+dp[u][j][0]+e.S*2)); } } 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]; tmp[i][0] = tmp[i][1] = inf; } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(tmp, 63, sizeof tmp); cin >> n >> k >> r; r--; for (int i = 1; i < n; i++) { int v, u, w; cin >> v >> u >> w; adj[--v].pb(mp(--u, w)); adj[u].pb(mp(v, w)); } dfs(); cout << dp[r][k][1] << '\n'; // for (int i = 1; i <= n; i++) cout << dp[r][i][1] << '\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...