# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
842253 | omeganot | 봉쇄 시간 (IOI23_closing) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1E9 + 7;
const int INF = 1E9; const ll INFLL = 1E18;
const int MAX = 2E5;
int N; int X; int Y; int K;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int C; cin >> C;
for(int test = 1; test <= C; test++) {
cin >> N >> X >> Y >> K;
vector<ll> dist(N, INFLL);
vector<vector<array<int, 2>>> adj(N);
for(int i = 0; i + 1 < N; i++) {
int U; int V; int W;
cin >> U >> V >> W;
U--; V--;
adj[U].push_back({V, W});
adj[V].push_back({U, W});
}
dist[X] = 0; dist[Y] = 0;
priority_queue<array<ll, 2>, vector<array<ll, 2>>, greater<array<ll, 2>>> pq; pq.push({0, X}); pq.push({0, Y});
while(pq.size()) {
array<ll, 2> x = pq.top();
if(x[0] != dist[x[1]]) {
continue;
}
for(array<int, 2> i : adj[x[1]]) {
if(dist[i[0]] > dist[x[1]] + i[1]) {
dist[i[0]] = dist[x[1]] + i[1];
pq.push({dist[i[0]], i[0]});
}
}
}
sort(dist.begin(), dist.end());
int ans = 0;
ll sum = 0;
for(ll i : dist) {
if(sum + i <= K) {
sum += i;
ans++;
}
}
cout << ans << "\n";
}
}