이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Evacuation plan"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n, m, q, k;
int spe[maxn], P[maxn];
vector<pair<int, int>> edges[maxn];
ll dist[maxn], ans[maxn];
void DJK() {
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>> > q;
memset(dist, 60, sizeof dist);
fi(i, 1, k) q.push({0, spe[i]}), dist[spe[i]] = 0;
while(!q.empty()) {
int u = q.top().second; ll g = q.top().first;
q.pop();
if(g > dist[u]) continue;
for(auto [v, w] : edges[u]) if(dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
q.push({dist[v], v});
}
}
}
struct DSU {
set<int> S[maxn];
int lab[maxn];
DSU() { memset(lab, -1, sizeof lab); }
int get_rt(int u) { return lab[u] < 0 ? u : lab[u] = get_rt(lab[u]); }
void uni(int u, int v, ll w) {
int p = get_rt(u), q = get_rt(v); if(p == q) return;
if(lab[p] > lab[q]) swap(p, q);
for(int x : S[q]) {
if(S[p].find(x) != S[p].end()) ans[x] = w, S[p].erase(x);
else S[p].insert(x);
}
S[q].clear();
lab[p] += lab[q], lab[q] = p;
}
} Dsu;
void solve()
{
cin >> n >> m;
fi(i, 1, m) {
int x, y, w; cin >> x >> y >> w;
edges[x].push_back({y, w});
edges[y].push_back({x, w});
}
cin >> k;
fi(i, 1, k) cin >> spe[i];
cin >> q;
fi(i, 1, q) {
int x, y; cin >> x >> y;
Dsu.S[x].insert(i), Dsu.S[y].insert(i);
}
DJK();
fi(i, 1, n) P[i] = i;
sort(P + 1, P + 1 + n, [](int x, int y) {
return dist[x] > dist[y];
});
fi(i, 1, n) {
int u = P[i];
for(auto [v, w] : edges[u]) if(dist[v] >= dist[u]) Dsu.uni(u, v, dist[u]);
}
fi(i, 1, q) cout << ans[i] << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'int main()':
plan.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |