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>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pi;
typedef vector<pi> pii;
typedef tuple<int,int,int> tii;
typedef vector<ll> li;
typedef vector<li> lii;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define LSOne(s) ((s)&(-s))
ll INF=1000000000000000010;
int inf=1e9+10;
ll M=1e9+7;
int n,m,k,q;
vector<vector<pair<int,ll>>> a,c;
pii ed;
vi b,p,vis;
vector<ll> dist;
ll dfs(int x,int y){
vis[x]=1;if(y==x)return INF;
for(auto u:c[x]){
if(vis[u.F])continue;
ll tmp=dfs(u.F,y);
if(tmp==-1)continue;
return min(tmp,u.S);
}
return -1;
}
int main() {
cin>>n>>m;
a.resize(n);
REP(i,0,m){
int x,y;ll z;
cin>>x>>y>>z;
x--;y--;
ed.PB({x,y});
a[x].PB({y,z});
a[y].PB({x,z});
}
cin>>k;
b.resize(k);
REP(i,0,k)cin>>b[i];
REP(i,0,k)b[i]--;
dist.resize(n,INF);
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> pq;
REP(i,0,k){
dist[b[i]]=0;
pq.push({0,b[i]});
}
while(!pq.empty()){
ll d=pq.top().F;
int x=pq.top().S;
pq.pop();
if(d!=dist[x])continue;
for(auto u:a[x]){
if(dist[u.F]<=dist[x]+u.S)continue;
dist[u.F]=dist[x]+u.S;
pq.push({dist[u.F],u.F});
}
}
c.resize(n);
priority_queue<tuple<ll,int,int>> pq2;
vis.resize(n,0);
vis[0]=1;
for(auto u:a[0])pq2.push({min(dist[0],dist[u.F]),0,u.F});
while(!pq2.empty()){
ll x=get<0>(pq2.top());
int y=get<1>(pq2.top());
int z=get<2>(pq2.top());
pq2.pop();
if(vis[z])continue;
c[y].PB({z,x});
c[z].PB({y,x});
vis[z]=1;
for(auto u:a[z])pq2.push({min(dist[z],dist[u.F]),z,u.F});
}
REP(i,0,n)cerr<<dist[i]<<" ";
cin>>q;
while(q--){
int x,y;cin>>x>>y;
x--;y--;
vis.clear();
vis.resize(n,0);
ll ans=dfs(x,y);
cout<<ans<<"\n";
}
}
# | 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... |