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,t;
vector<vector<pair<int,ll>>> a,c;
pii ed;
vi b,p,vis,sa,se;
vector<ll> dist;
vector<vector<ll>> mn;
int sval=0,eval=0;
vii par;
void dfs(int x){
sa[x]=sval++;
for(auto u:c[x]){
if(sa[u.F]!=-1)continue;
par[u.F][0]=x;
dfs(u.F);
}
se[x]=eval++;
}
bool ances(int x,int y){
if(sa[x]>=sa[y]&&se[x]<=se[y])return 1;
return 0;
}
ll solve(int x,int y){
int z=y;
ll ans=mn[y][0];
for(int i=t-1;i>=0;i--)if(par[y][i]!=-1&&!ances(x,par[y][i])){
ans=min(ans,mn[y][i]);
y=par[y][i];
}
return min(ans,mn[y][0]);
}
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});
}
sa.resize(n,-1);se.resize(n,-1);
t=log2(n)+1;
par.resize(n,vi(t,-1));mn.resize(n,vector<ll>(t,INF));
dfs(0);
REP(i,0,n)if(par[i][0]!=-1)mn[i][0]=dist[par[i][0]];
REP(j,1,t)REP(i,0,n)if(par[i][j-1]!=-1)par[i][j]=par[par[i][j-1]][j-1];
REP(j,1,t)REP(i,0,n)if(par[i][j-1]!=-1)mn[i][j]=min(mn[i][j-1],mn[par[i][j-1]][j-1]);
cin>>q;
while(q--){
int x,y;cin>>x>>y;
x--;y--;
ll ans=min(solve(x,y),solve(y,x));
ans=min(ans,dist[x]);
ans=min(ans,dist[y]);
cout<<ans<<"\n";
}
}
Compilation message (stderr)
plan.cpp: In function 'll solve(int, int)':
plan.cpp:49:9: warning: unused variable 'z' [-Wunused-variable]
49 | int z=y;
| ^
# | 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... |