#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define pb push_back
#define pii pair<int,int>
#define sz size()
#define all(v) v.begin(),v.end()
#define fi first
#define se second
const int N = 1e5;
const int mod = 1e9+7;
const int INF = 1e9;
const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};
int n,m,k;
vector<pii> g[N+1];
set<int> st[N+1];
int ans[N+1],p[N+1];
int get(int v){
if(v!=p[v])
p[v]=get(p[v]);
return p[v];
}
void merge(int a,int b,int val){
a=get(a);
b=get(b);
if(a!=b){
if(st[a].sz<st[b].sz)
st[a].swap(st[b]);
for(auto i: st[b]){
if(st[a].count(i)){
ans[i]=val;
st[a].erase(i);
}
else
st[a].insert(i);
}
p[b]=a;
}
}
void solve() {
cin>>n>>m;
for(int i = 1; i <= m; i++){
int x,y,c;
cin>>x>>y>>c;
g[x].pb({y,c});
g[y].pb({x,c});
}
cin>>k;
set<pii> st_d;
int d[n+1];
for(int i = 1; i <= n; i++){
d[i]=INF;
p[i]=i;
}
for(int i = 1; i <= k; i++){
int x;
cin>>x;
d[x]=0;
st_d.insert({d[x],x});
}
int q;
cin>>q;
for(int i = 1; i <= q; i++){
int x,y;
cin>>x>>y;
st[x].insert(i);
st[y].insert(i);
}
while(!st_d.empty()){
int v=st_d.begin()->se;
st_d.erase(st_d.begin());
for(auto [to,c]: g[v]){
if(d[to]>d[v]+c){
st_d.erase({d[to],to});
d[to]=d[v]+c;
st_d.insert({d[to],to});
}
}
}
vector<pair<int,pii>> v;
for(int i = 1; i <= n; i++){
for(auto [val,j]: g[i]){
if(d[i]!=0 && d[j]!=0){
v.pb({min(d[i],d[j]),{i,j}});
}
}
}
sort(v.rbegin(),v.rend());
for(auto i: v){
int c=i.fi,x=i.se.fi,y=i.se.se;
merge(x,y,c);
}
for(int i = 1; i <= q; i++){
cout<<ans[i]<<"\n";
}
}
int main() {
//freopen("cowrun.in","r",stdin);
//freopen("cowrun.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--) {
solve();
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |