This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pi pair<int, int>
#define ti tuple<int, int, int>
using namespace std;
int n, m, dis[100010], rt[100010], ans[100010], u, v, w, q, cu, cv, cw, s, t;
vector<pi> g[100010];
vector<int> st, en;
set<int> pa[100010];
priority_queue<pi, vector<pi>, greater<pi>> pq;
priority_queue<ti> ed;
int fr(int no) {
if(rt[no] == no) return no;
else return rt[no] = fr(rt[no]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
while(m--) {
cin >> u >> v >> w;
g[u].push_back({w, v});
g[v].push_back({w, u});
st.push_back(u);
en.push_back(v);
}
fill(dis, dis+100010, 1e9);
cin >> q;
while(q--) {
cin >> u;
pq.push({0, u});
}
cin >> q;
for(int i=1; i<=q; i++) {
cin >> s >> t;
pa[s].insert(i);
pa[t].insert(i);
}
while(!pq.empty()) {
cu = pq.top().second;
cw = pq.top().first;
pq.pop();
if(dis[cu] <= cw) {
continue;
}
dis[cu] = cw;
for(auto i: g[cu]) {
pq.push({i.first+cw, i.second});
}
}
for(int i=0; i<st.size(); i++) {
ed.push({min(dis[st[i]], dis[en[i]]), st[i], en[i]});
}
for(int i=0; i<=n; i++) {
rt[i] = i;
}
while(!ed.empty()) {
cw = get<0>(ed.top());
cu = fr(get<1>(ed.top()));
cv = fr(get<2>(ed.top()));
ed.pop();
if(rt[cu] == rt[cv]) continue;
if(pa[cu].size() < pa[cv].size()) {
swap(cu, cv);
}
for(auto i: pa[cv]) {
if(pa[cu].count(i)) {
ans[i] = cw;
pa[cu].erase(i);
} else {
pa[cu].insert(i);
}
}
rt[cv] = cu;
}
for(int i=1; i<=q; i++) {
cout << ans[i] << "\n";
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:54:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for(int i=0; i<st.size(); i++) {
| ~^~~~~~~~~~
# | 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... |