#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 100005;
int n, m, k, q;
pii upit[mxN];
vector<pii> g[mxN];
int dist[mxN];
int L[mxN];
int R[mxN];
int ans[mxN];
vector<int> vec[mxN];
int sz[mxN];
int par[mxN];
int findpar(int x){
return (x == par[x] ? x : par[x] = findpar(par[x]));
}
void unite(int x, int y){
x = findpar(x), y = findpar(y);
if(x == y)return;
if(sz[x] < sz[y])swap(x, y);
par[y] = x; sz[x] += sz[y];
}
int id[mxN];
bool cmp(int i, int j){
return dist[i] > dist[j];
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
ff(i,1,m){
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
ff(i,1,n)dist[i] = inf;
cin >> k; priority_queue<pii,vector<pii>,greater<pii>> pq;
ff(i,1,k){
int g;
cin >> g;
pq.push({dist[g] = 0, g});
}
while(sz(pq)){
pii v = pq.top(); pq.pop();
if(dist[v.se] < v.fi)continue;
for(auto c : g[v.se]){
int u = c.fi;
int w = c.se;
if(dist[u] > w + v.fi){
pq.push({dist[u] = w + v.fi, u});
}
}
}
cin >> q;
ff(i,1,q){
cin >> upit[i].fi >> upit[i].se;
L[i] = 1; R[i] = n; ans[i] = 0;
}
ff(i,1,n)id[i] = i;
sort(id + 1, id + n + 1, cmp);
ff(_,1,20){
ff(i,1,n){
par[i] = i;
sz[i] = 1;
vec[i].clear();
}
ff(i,1,q){
if(L[i] <= R[i]){
int mid = (L[i] + R[i]) / 2;
vec[mid].pb(i);
}
}
ff(i,1,n){
int v = id[i];
for(auto c : g[v]){
if(dist[c.fi] >= dist[v]){
unite(c.fi, v);
}
}
for(auto c : vec[i]){
int s = upit[c].fi;
int t = upit[c].se;
if(findpar(s) == findpar(t)){
R[c] = i - 1;
ans[c] = i;
}
else{
L[c] = i + 1;
}
}
}
}
ff(i,1,q)cout << dist[id[ans[i]]] << '\n';
return 0;
}
/*
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
// probati bojenje sahovski
*/
# | 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... |