#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
const int maxn=1e5;
vector<pii> adj[maxn+1];
int par[maxn+1],ls,ans[maxn+1];
bool en[maxn+1];
set<int> s[maxn+1];
int rep(int x){
if(x==par[x])return x;
return par[x]=rep(par[x]);
}
void join(int x,int y){
x=rep(x);y=rep(y);
if(x==y)return;
if(s[x]>s[y])swap(x,y);
par[x]=y;
for(int i:s[x]){
if(s[y].count(i))ans[i]=ls;
else s[y].insert(i);
}
}
void solve() {
int n,m;
cin>>n>>m;
while(m--){
int u,v,w;
cin>>u>>v>>w;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
bool vis[n+1];
pii d[n+1];
priority_queue<pii> pq;
for(int i=1;i<=n;i++){
d[i]={1e9,i};
vis[i]=0;
}
int k;
cin>>k;
while(k--){
int x;
cin>>x;
d[x].fs=0;
pq.push({0,x});
}
while(!pq.empty()){
int v=pq.top().sc;
pq.pop();
if(vis[v])continue;
vis[v]=1;
for(auto[u,w]:adj[v])if(!vis[u]){
d[u].fs=min(d[u].fs,d[v].fs+w);
pq.push({-d[u].fs,u});
}
}
int q;
cin>>q;
for(int i=0;i<q;i++){
int u,v;
cin>>u>>v;
s[u].insert(i);
s[v].insert(i);
}
iota(par+1,par+n+1,1);
sort(d+1,d+n+1);
reverse(d+1,d+n+1);
for(int i=1;i<=n;i++){
auto[ds,v]=d[i];
ls=ds;
en[v]=1;
for(auto[u,w]:adj[v])if(en[u])join(u,v);
}
for(int i=0;i<q;i++)cout<<ans[i]<<'\n';
}
int main() {
#ifdef FPO
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# | 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... |