This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int>vi;
typedef vector<vector<int>>vvi;
typedef vector<ll>vl;
typedef vector<vl> vvl;
typedef pair<int,int>pi;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<ld> vld;
typedef pair<ld,ld> pld;
typedef vector<pi> vpi;
//typedef tree<ll, null_type, less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
template<typename T> ostream& operator<<(ostream& os, vector<T>& a){os<<"[";for(int i=0; i<ll(a.size()); i++){os << a[i] << ((i!=ll(a.size()-1)?" ":""));}os << "]\n"; return os;}
#define all(x) x.begin(),x.end()
#define YES out("YES")
#define NO out("NO")
#define out(x){cout << x << "\n"; return;}
#define outfl(x){cout << x << endl;return;}
#define GLHF ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define print(x){for(auto ait:x) cout << ait << " "; cout << "\n";}
#define pb push_back
#define umap unordered_map
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1, T2>& p){is >> p.first >> p.second;return is;}
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& p){os <<"" << p.first << " " << p.second << ""; return os;}
void usaco(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
}
template<typename T>
void read(vector<T>& v){
int n=v.size();
for(int i=0; i<n; i++)
cin >> v[i];
}
template<typename T>
vector<T>UNQ(vector<T>a){
vector<T>ans;
for(T t:a)
if(ans.empty() || t!=ans.back())
ans.push_back(t);
return ans;
}
void solve();
int main(){
GLHF;
int t=1;
//cin >> t;
while(t--)
solve();
}
vi par;
int get(int x){return (x==par[x]?x:get(par[x]));}
void unite(int u,int v){par[get(u)]=get(v);}
vector<vpi> build(vvi E,int n){
int edges=0;
sort(all(E),greater<vi>());
par.resize(n+1);
for(int i=1; i<=n; i++)
par[i]=i;
vector<vpi> g(n+1);
for(vi vv:E){
int u=vv[1],v=vv[2];
if(get(u)==get(v))continue;
unite(u,v);
edges++;
int w=vv[0];
g[u].pb({v,w});
g[v].pb({u,w});
if(edges==n-1)break;
}
return g;
}
vvi up,mn;
vector<vpi>g;
int lg=19;
vi dep;
void dfs(int x,int p){
up[x][0]=p;
if(p!=-1)dep[x]=dep[p]+1;
for(int j=1; j<lg; j++)
if(up[x][j-1]!=-1) {
up[x][j] = up[up[x][j - 1]][j - 1];
mn[x][j] = min(mn[x][j - 1], mn[up[x][j - 1]][j - 1]);
}
for(pi nbr:g[x])
if(nbr.first!=p){
mn[nbr.first][0]=nbr.second;
dfs(nbr.first,x);
}
}
int lca(int u,int v){
if(dep[u]>dep[v])swap(u,v);
for(int i=0; i<lg ;i++)
if((dep[v]-dep[u])&(1<<i))
v=up[v][i];
for(int i=lg-1; 0<=i;i --)
if(up[u][i]!=up[v][i])
u=up[u][i],v=up[v][i];
if(u!=v)u=up[u][0];
return u;
}
int qur(int x,int k){
int ans=1e9;
for(int i=0; i<lg; i++)
if(k&(1<<i)){
ans=min(ans,mn[x][i]);
x=up[x][i];
}
return ans;
}
void solve() {
int n,m;
cin >> n >> m;
g.resize(n+1);
vvi E;
for(int i=0; i<m; i++){
int u,v,w;
cin >> u >> v >> w;
assert(w<=1000);
g[u].pb({v,w});
g[v].pb({u,w});
E.pb({w,u,v});
}
priority_queue<vi,vvi,greater<vi>>pq;
int k;
cin >> k;
for(int i=0; i<k; i++){
int x;
cin >> x;
pq.push({0,x});
}
vector<bool>vis(n+1);
vi a(n+1);
while(pq.size()){
vi v=pq.top();
pq.pop();
int x=v[1],w=v[0];
if(vis[x])continue;
vis[x]=1;
a[x]=w;
for(pi nbr:g[x])
if(!vis[nbr.first] && w+nbr.second>0)
pq.push({w+nbr.second,nbr.first});
}
for(vi& v:E)
v[0]=min(a[v[1]],a[v[2]]);
g=build(E,n);
up.resize(n+1,vi(lg,-1));
mn.resize(n+1,vi(lg,-1));
dep.resize(n+1);
dfs(1,-1);
int q;
cin >> q;
while(q--){
int s,t;
cin >> s >> t;
int l=lca(s,t);
int ans=qur(s,dep[s]-dep[l]);
ans=min(ans,qur(t,dep[t]-dep[l]));
cout << ans << "\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
*/
Compilation message (stderr)
plan.cpp: In function 'void usaco(std::string)':
plan.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen(FIN, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~
plan.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen(FOUT, "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... |