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>
#define all(x) x.begin(),x.end()
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define f first
#define s second
#define left(v) v + v
#define right(v) v + v + 1
#define ub upper_bound
#define lb lower_bound
using namespace std;
typedef long long ll;
//17 SEVENTEEN
const long double Pi = acos(-1.0);
const ll dx[] = {0,0,1,-1};
const ll dy[] = {1,-1,0,0};
const ll N = (ll) 1e6 + 17;
const ll M = (ll) 5e3 + 69;
const ll inf = (ll) 1e18 + 3;
const ll mod = (ll) 1e9 + 7;
ll sq(ll x) { return x * x; }
ll zxc = 1, mal[N];
ll n, m, k, q, u, v, w;
ll a, b, x, used[N], d[M][M];
vector<ll> g[N];
bool gay(ll v, ll x) {
for(ll i = 1; i <= k; i++) {
if(d[v][mal[i]] < x) return 0;
}
return 1;
}
void dfs(ll v, ll x) {
used[v] = 1;
for(ll to : g[v]) {
if(!used[to] && gay(to, x)) dfs(to, x);
}
}
ll check(ll x) {
for(ll i = 1; i <= n; i++) used[i] = 0;
if(gay(a, x)) dfs(a, x);
return used[b];
}
void solve() {
cin >> n >> m;
for(ll i = 1; i <= n; i++) {
for(ll j = 1; j <= n; j++) {
if(i != j) d[i][j] = inf;
}
}
for(ll i = 1; i <= m; i++) {
cin >> u >> v >> w;
d[u][v] = w, d[v][u] = w;
g[u].pb(v), g[v].pb(u);
}
cin >> k;
for(ll i = 1; i <= k; i++) {
cin >> mal[i];
}
for(ll k = 1; k <= n; k++)
for(ll i = 1; i <= n; i++)
for(ll j = 1; j <= n; j++)
if(d[i][k] < inf && d[k][j] < inf)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
// for(ll i = 1; i <= n; i++) {
// for(ll j = 1; j <= n; j++) {
// cout << d[i][j] << " ";
// }
// cout << "\n";
// }
cin >> q;
while(q--) {
cin >> a >> b;
ll l = 0, r = inf;
while(l < r) {
ll m = (l + r + 1) / 2;
if(check(m)) l = m;
else r = m - 1;
}
cout << l << "\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
*/
int main(/*Уверенно*/) {
ios_base::sync_with_stdio(0);
cin.tie(0);
/*
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
*/
// cin >> zxc;
while(zxc--) {
solve();
}
return 0;
}
// さよならさ いかなくちゃ
# | 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... |