# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1126864 | ntdaccode | Museum (CEOI17_museum) | C++20 | 827 ms | 785232 KiB |
#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int N = 1e4 + 10;
int n, k, root, f[2][N][N], g[2][N];
vector<ii> ke[N];
int sz[N];
void dfs(int u,int p = 0)
{
sz[u] = 1;
for(int i = 0;i <= 1; i++) f[i][u][1] = f[i][u][0] = 0;
for(ii tmp : ke[u]) {
int v = tmp.first, w = tmp.second;
if(v == p) continue;
dfs(v,u);
for(int i = 0;i <= sz[u] + sz[v]; i++) g[0][i] = g[1][i] = 1e9;
for(int i = 1;i <= min(k,sz[u]); i++) {
for(int j = 0;j <= min(k,sz[v]); j++) {
if(i + j > k) break;
g[0][i + j] = min(g[0][i + j], f[0][u][i] + f[0][v][j] + 2 * w);
g[1][i + j] = min(g[1][i + j], f[0][u][i] + f[1][v][j] + w);
g[1][i + j] = min(g[1][i + j], f[1][u][i] + f[0][v][j] + 2 * w);
}
}
sz[u] += sz[v];
for(int i = 1;i <= sz[u]; i++){
for(int j = 0;j <= 1; j++) f[j][u][i] = min(g[j][i], f[j][u][i]);
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r")) {
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k >> root;
for(int i = 1;i <= n - 1; i++) {
int u,v,w;
cin >> u >> v >> w;
ke[u].pb({v,w});
ke[v].pb({u,w});
}
for(int i = 1;i <= n; i++) {
for(int j = 0;j <= n; j++) {
for(int e = 0;e <= 1; e++) f[e][i][j] = 1e9;
}
}
dfs(root,0);
cout << f[1][root][k];
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |