# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
964437 |
2024-04-16T20:55:38 Z |
dio_2 |
Museum (CEOI17_museum) |
C++17 |
|
1868 ms |
787772 KB |
#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;
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 dfs2(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;
dfs2(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;
}
}
dfs2(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;
}
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 time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2392 KB |
Output is correct |
2 |
Correct |
14 ms |
2548 KB |
Output is correct |
3 |
Correct |
5 ms |
2392 KB |
Output is correct |
4 |
Correct |
8 ms |
2396 KB |
Output is correct |
5 |
Correct |
16 ms |
2512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
544 ms |
786784 KB |
Output is correct |
2 |
Correct |
352 ms |
786880 KB |
Output is correct |
3 |
Correct |
325 ms |
787772 KB |
Output is correct |
4 |
Correct |
303 ms |
787236 KB |
Output is correct |
5 |
Correct |
308 ms |
787104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
544 ms |
786784 KB |
Output is correct |
2 |
Correct |
352 ms |
786880 KB |
Output is correct |
3 |
Correct |
325 ms |
787772 KB |
Output is correct |
4 |
Correct |
303 ms |
787236 KB |
Output is correct |
5 |
Correct |
308 ms |
787104 KB |
Output is correct |
6 |
Incorrect |
1868 ms |
787052 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2392 KB |
Output is correct |
2 |
Correct |
14 ms |
2548 KB |
Output is correct |
3 |
Correct |
5 ms |
2392 KB |
Output is correct |
4 |
Correct |
8 ms |
2396 KB |
Output is correct |
5 |
Correct |
16 ms |
2512 KB |
Output is correct |
6 |
Correct |
544 ms |
786784 KB |
Output is correct |
7 |
Correct |
352 ms |
786880 KB |
Output is correct |
8 |
Correct |
325 ms |
787772 KB |
Output is correct |
9 |
Correct |
303 ms |
787236 KB |
Output is correct |
10 |
Correct |
308 ms |
787104 KB |
Output is correct |
11 |
Incorrect |
1868 ms |
787052 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |