# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1111864 | luvna | Museum (CEOI17_museum) | C++14 | 225 ms | 140624 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 1e4+15;
const int MOD = 1e9 + 7;
int n, k, root;
vector<pair<int,int>> adj[N];
vector<int> f[N], g[N];
int sz[N];
void dfs(int u, int p){
f[u].resize(sz[u] + 1, MOD);
g[u].resize(sz[u] + 1, MOD);
f[u][1] = g[u][1] = 0;
int siz = 1;
for(auto [v, w] : adj[u]) if(v != p){
dfs(v,u);
for(int i = siz; i >= 1; i--){
for(int j = sz[v]; j >= 1; j--){
f[u][i+j] = min({f[u][i+j], f[u][i] + g[v][j] + 2*w, g[u][i] + f[v][j] + w});
g[u][i+j] = min(g[u][i+j], g[u][i] + g[v][j] + 2*w);
}
}
siz += sz[v];
}
}
void luvna(int u, int p){
sz[u] = 1;
for(auto [v, w] : adj[u]) if(v != p){
luvna(v,u);
sz[u] += sz[v];
}
}
void solve(){
cin >> n >> k >> root;
for(int i = 1; i < n; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
luvna(root,-1);
dfs(root,-1);
cout << min(g[root][k], f[root][k]);
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
컴파일 시 표준 에러 (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... |