제출 #964436

#제출 시각아이디문제언어결과실행 시간메모리
964436dio_2Museum (CEOI17_museum)C++17
20 / 100
16 ms2908 KiB
#include<bits/stdc++.h> using namespace std; using pii = pair<int, int>; template <typename T> void min_self(T &a, T b) { if(b < a) a = b; } const int N = 10010; const int inf = (int)1e9; int dp[N][N]; vector<pii> g[N]; int n, k, x; /* 25 Pts int f[N][N][2]; #define come_back 0 #define leave 1 void dfs(int u, int p = -1){ f[u][1][come_back] = f[u][1][leave] = 0; vector<pii> ch; for(auto [v, c] : g[u]) if(v != p){ dfs(v, u); ch.emplace_back(v, c); } if(ch.empty()) return ; if((int)ch.size() == 1){ for(auto [v, c] : ch){ for(int vis = 1;vis < k;vis++){ // k <= 100. f[u][vis + 1][leave] = c + min(f[v][vis][leave], f[v][vis][come_back]); f[u][vis + 1][come_back] = 2 * c + f[v][vis][come_back]; } } } else if((int)ch.size() == 2){ for(int vis = 1;vis < k;vis++){ for(int vis1 = 0;vis1 <= vis;vis1++){ int vis2 = vis - vis1; auto [v1, c1] = ch[0]; auto [v2, c2] = ch[1]; c1 = (vis1 > 0 ? c1 : 0); c2 = (vis2 > 0 ? c2 : 0); min_self(f[u][vis + 1][come_back], 2 * c1 + 2 * c2 + f[v1][vis1][come_back] + f[v2][vis2][come_back]); min_self(f[u][vis + 1][leave], 2 * c1 + c2 + f[v1][vis1][come_back] + min(f[v2][vis2][leave], f[v2][vis2][come_back])); min_self(f[u][vis + 1][leave], 2 * c2 + c1 + f[v2][vis2][come_back] + min(f[v1][vis1][leave], f[v1][vis1][come_back])); } } } else { for(int vis = 1;vis < k;vis++){ for(int vis1 = 0;vis1 <= vis;vis1++){ for(int vis2 = 0;vis2 + vis1 <= vis;vis2++){ int vis3 = vis - vis1 - vis2; assert(vis1 + vis2 + vis3 == vis); auto [v1, c1] = ch[0]; auto [v2, c2] = ch[1]; auto [v3, c3] = ch[2]; c1 = (vis1 > 0 ? c1 : 0); c2 = (vis2 > 0 ? c2 : 0); c3 = (vis3 > 0 ? c3 : 0); min_self(f[u][vis + 1][come_back], 2 * (c1 + c2 + c3) + f[v1][vis1][come_back] + f[v2][vis2][come_back] + f[v3][vis3][come_back]); min_self(f[u][vis + 1][leave], 2 * c1 + 2 * c2 + c3 + f[v1][vis1][come_back] + f[v2][vis2][come_back] + min(f[v3][vis3][leave], f[v3][vis3][come_back])); min_self(f[u][vis + 1][leave], 2 * c2 + 2 * c3 + c1 + f[v2][vis2][come_back] + f[v3][vis3][come_back] + min(f[v1][vis1][leave], f[v1][vis1][come_back])); min_self(f[u][vis + 1][leave], 2 * c1 + 2 * c3 + c2 + f[v1][vis1][come_back] + f[v3][vis3][come_back] + min(f[v2][vis2][leave], f[v2][vis2][come_back])); } } } } } */ bool inMask[21]; int dep[21]; bool onNode(int mask, int node){ return (mask >> (node - 1)) & 1; } int sum; void dfs(int u, int p = -1){ for(auto [v, c] : g[u]) if(v != p){ if(!inMask[v]) continue; sum += 2 * c; dep[v] = dep[u] + c; dfs(v, u); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); //~ for(int i = 0;i < N;i++) for(int j = 1;j < N;j++) dp[i][j] = inf; cin >> n >> k >> x; for(int i = 1;i <= n - 1;i++){ int a, b, c; cin >> a >> b >> c; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } if(n <= 20){ auto Do = [&](int mask)->int{ fill(inMask, inMask + 21, false); fill(dep, dep + 21, 0); sum = 0; for(int i = 1;i <= n;i++){ if(onNode(mask, i)){ inMask[i] = 1; } } dfs(x); int mx = 0; for(int i = 1;i <= n;i++){ if(inMask[i] && dep[i] == 0 && i != x) return inf; if(inMask[i]) mx = max(mx, dep[i]); } return sum - mx; }; int ans = inf; for(int mask = 0;mask < (1 << n);mask++){ if(__builtin_popcount(mask) != k || !onNode(mask, x)) continue; ans = min(ans, Do(mask)); } cout << ans << '\n'; return 0; } /* 25 pts for(int a = 0;a < N;a++) for(int b = 1;b < N;b++) for(int c = 0;c < 2;c++) f[a][b][c] = inf; dfs(x); cout << min(f[x][k][come_back], f[x][k][leave]) << '\n'; */ return 0; } /* 10 9 1 1 2 3 2 5 1 2 6 1 1 3 4 3 7 2 3 8 100 1 4 4 4 9 1 4 10 1 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...