이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second
const int mxn = 10000;
int n, k, x;
int p[mxn], vis[mxn];
vector<pi> g[mxn];
priority_queue<pi, vector<pi>, greater<pi>> pq;
void dfs(int c){
for(pi i : g[c]){
if(i.f == p[c]) continue;
p[i.f] = c;
dfs(i.f);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> x;
x--;
for(int i = 0; i < n - 1; i++){
int u, v, w;
cin >> u >> v >> w;
u--, v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
p[x] = -1;
dfs(x);
int ret = 0x3f3f3f3f;
for(int i = 0; i < n; i++){
int cnt = k, cur = 0;
memset(vis, 0, sizeof(vis));
while(!pq.empty()) pq.pop();
for(int j = i; ~j; j = p[j]){
cnt--, vis[j] = 1;
for(pi l : g[j]){
if(l.f == p[j]) cur += l.s;
else if(!vis[l.f]) pq.push({2 * l.s, l.f});
}
}
while(cnt > 0){
cur += pq.top().f;
int c = pq.top().s;
pq.pop();
cnt--, vis[c] = 1;
for(pi j : g[c]){
if(!vis[j.f]) pq.push({2 * j.s, j.f});
}
}
ret = min(ret, cur);
}
cout << ret << endl;
return 0;
}
| # | 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... |