이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
ID: gurban1
LANG: C++
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn=1e5+5;
int n,m,k,qr,p[maxn];
int dis[maxn],vis[maxn];
int l[maxn],r[maxn],ans[maxn];
vector<int>md[maxn];
vector<pair<int,int>>E[maxn],nd;
priority_queue<pair<int,int>>q;
int ata(int x){
if(p[x] == x) return x;
return p[x] = ata(p[x]);
}
void un(int a,int b){
int x = ata(a),y=ata(b);
if(x != y) p[y] = x;
}
int main(){
// freopen("","r",stdin);
// freopen("","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 1;i <= m;i++){
int x,y,w; cin >> x >> y >> w;
E[x].push_back({y,w});
E[y].push_back({x,w});
}
for(int i = 1;i <= n;i++) dis[i] = 1e9;
cin >> k;
for(int i = 1;i <= k;i++){
int x; cin >> x;
dis[x] = 0;
q.push({0,x});
}
while(!q.empty()){
int x = q.top().second;
q.pop();
if(vis[x]) continue;
vis[x] = 1;
for(auto i : E[x]){
if(dis[i.first] > dis[x] + i.second){
dis[i.first] = dis[x] + i.second;
q.push({-dis[i.first],i.first});
}
}
}
for(int i = 1;i <= n;i++) nd.push_back({dis[i],i});
sort(nd.begin(),nd.end());
reverse(nd.begin(),nd.end());
nd.insert(nd.begin(),{0,0});
cin >> qr;
vector<pair<int,int>>v(qr+1);
for(int i = 1;i <= qr;i++){
cin >> v[i].first >> v[i].second;
l[i] = 1,r[i] = n;
}
// for(int i = 1;i <= n;i++) cout<<nd[i].first<<' '<<nd[i].second<<'\n';
for(int tt = 0;tt <= 22;tt++){
for(int i = 1;i <= n;i++) md[i].clear(),p[i] = i,vis[i]=0;
for(int i = 1;i <= qr;i++){
if(l[i] > r[i]) continue;
md[(l[i]+r[i])/2].push_back(i);
}
for(int i = 1;i <= n;i++){
vis[nd[i].second] = 1;
for(auto j : E[nd[i].second]) if(vis[j.first]) un(nd[i].second,j.first);
for(auto j : md[i]){
if(ata(v[j].first) == ata(v[j].second)){
ans[j] = nd[i].first;
r[j] = i - 1;
}
else l[j] = i + 1;
}
}
}
for(int i = 1;i <= qr;i++) cout<<ans[i]<<'\n';
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
1
5 3
*/
# | 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... |