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 int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=1e5+5;
vector <pair <int,int > > g[N];
int no[N],d[N];
void find(int s,int n){
set <pair <int,int> > st;
st.insert({0,s});
for(int i=1;i<=n;i++)d[i]=1e18;
d[s]=0;
while(!st.empty()){
int v=st.begin()->ss;
st.erase(st.begin());
for(auto to : g[v]){
if(d[to.ff]>d[v]+to.ss){
st.erase({d[to.ff],to.ff});
d[to.ff]=d[v]+to.ss;
st.insert({d[to.ff],to.ff});
}
}
}
}
int get(int s,int t,int n){
set <pair <int,int> > st;
st.insert({0,s});
vector <int> dist(n+1,1e18);
vector <int> p(n+1);
dist[s]=0;
while(!st.empty()){
int v=st.begin()->ss;
st.erase(st.begin());
for(auto to : g[v]){
if(to.ff==0)continue;
if(dist[to.ff]>dist[v]+to.ss){
p[to.ff]=v;
st.erase({dist[to.ff],to.ff});
dist[to.ff]=dist[v]+to.ss;
st.insert({dist[to.ff],to.ff});
}
}
}
vector <int> path;
path.pb(t);
while(t!=s){
t=p[t];
path.pb(t);
}
int mn=1e18;
for(auto x : path)mn=min(mn,d[x]);
if(mn==1e18)mn=0;
return mn;
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int u,v,w;
cin>>u>>v>>w;
g[u].pb({v,w});
g[v].pb({u,w});
}
int k;
cin>>k;
for(int i=0;i<k;i++){
int x;
cin>>x;
no[x]=1;
g[0].pb({x,0});
g[x].pb({0,0});
}
find(0,n);
int q;
cin>>q;
while(q--){
int s,t;
cin>>s>>t;
cout<<get(s,t,n)<<"\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
*/
# | 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... |