This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author lox:( not mine
#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define popcnt __builtin_popcount
#define all(a) a.begin(),a.end()
#define low_b lower_bound
#define up_b upper_bound
#define vii vector<pair<ll,ll>>
#define vll vector<pair<ll,ll>>
#define pii pair<ll,ll>
#define mii map<ll,ll>
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define vi vector<ll>
#define vl vector<ll>
#define pb push_back
#define endl '\n'
#define Y second
#define X first
typedef long double ld;
typedef long long ll;
const long long dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},col=107,N=1e5+777;
const ll mod=1e9+7,inf=1e18;
void fre_open(){freopen("file.in","r",stdin);freopen("file.out","w",stdout);}
ll binpow(ll n,ll k){if(k<0)return 0ll;if(k==0)return 1ll;if(k%2)return (binpow(n,k-1)*n)%mod;ll kk=binpow(n,k/2)%mod;return (kk*kk)%mod;}
int n,m,k,s,t,ans,Q,d[N],e[N];
int p[N],sz[N];
int timer,tin[N],tout[N],up[N][19],mn[N][19],di[N];
vector<pii>g[N],gr[N];
bool used[N];
int get(int v) {
if(p[v]==v) return v;
return p[v]=get(p[v]);
}
void comb(int u,int v) {
u=get(u);
v=get(v);
if(sz[u]>sz[v]) swap(u,v);
p[u]=v;
sz[v]+=sz[u];
}
void calc(int v,int pr,int cst) {
tin[v]=++timer;
up[v][0]=pr;
mn[v][0]=cst;
for(int i=1; i<=18; ++i) {
up[v][i]=up[up[v][i-1]][i-1];
mn[v][i]=min(mn[v][i-1],mn[up[v][i-1]][i-1]);
}
for(auto [to, w]:gr[v]) {
if(to!=pr){
di[to]=di[v]+1;
calc(to,v,w);
}
}
tout[v]=++timer;
}
bool is_parent(int u,int v) {return (tin[u]<=tin[v] && tout[u]>=tout[v]);}
int lca(int u,int v) {
if(is_parent(u,v)) return u;
if(is_parent(v,u)) return v;
for(int i=18; i>=0; --i) if(!is_parent(up[u][i],v)) u=up[u][i];
return up[u][0];
}
int get(int u,int v) {
int k=lca(u,v),x=di[u]-di[k],y=di[v]-di[k];
int res=inf;
for(int i=18; i>=0; --i) {
if(x>>i&1) res=min(res,mn[u][i]),u=up[u][i];
if(y>>i&1) res=min(res,mn[v][i]),v=up[v][i];
}
return res;
}
void solve(){
cin>>n>>m;
vector<pii>edge;
for(int i=1; i<=n; ++i) sz[i]=1,p[i]=i;
for(int i=1; i<=m; ++i){
int u,v,w;
cin>>u>>v>>w;
edge.pb({u,v});
g[u].pb({v,w});
g[v].pb({u,w});
}
cin>>k;
for(int i=1; i<=n; ++i) d[i]=inf;
set<pii>q;
for(int i=1; i<=k; ++i) {
int x;
cin>>x;
d[x]=0;
q.insert({0,x});
}
while(!q.empty()) {
int v=(*q.begin()).Y;
q.erase(q.begin());
for(auto [to,w]:g[v]) {
if(d[v]+w<d[to]) {
q.erase({d[to],to});
d[to]=d[v]+w;
q.insert({d[to],to});
}
}
}
vector<pair<int,pii>>po;
for(auto [u,v]:edge)po.pb({min(d[u],d[v]),{u,v}});
sort(all(po));
for(int i=0; i<m; ++i) {
int u=po[i].Y.X,v=po[i].Y.Y;
if(get(u)!=get(v)) {
gr[u].pb({v,po[i].X});
gr[v].pb({u,po[i].X});
comb(u,v);
}
}
calc(1,1,inf);
cin>>Q;
while(Q--) {
cin>>s>>t;
cout<<get(s,t)<<endl;
}
}
int main() {
//fre_open();
speed;
ll tt=1;
//cin>>tt;
while(tt--) solve();
}
/*
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*/
Compilation message (stderr)
plan.cpp: In function 'int get(int, int)':
plan.cpp:70:10: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '1000000000000000000' to '-1486618624' [-Woverflow]
70 | int res=inf;
| ^~~
plan.cpp: In function 'void solve()':
plan.cpp:89:31: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '1000000000000000000' to '-1486618624' [-Woverflow]
89 | for(int i=1; i<=n; ++i) d[i]=inf;
| ^~~
plan.cpp:119:11: warning: overflow in conversion from 'll' {aka 'long long int'} to 'int' changes value from '1000000000000000000' to '-1486618624' [-Woverflow]
119 | calc(1,1,inf);
| ^~~
plan.cpp: In function 'void fre_open()':
plan.cpp:27:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | void fre_open(){freopen("file.in","r",stdin);freopen("file.out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
plan.cpp:27:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | void fre_open(){freopen("file.in","r",stdin);freopen("file.out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |