#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int N = 2e5 + 7;
const int R = 1e9 + 10;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const int B = 320;
vector < pair < int, int >> g[N];
bool used[N];
ll dist[N];
void solve() {
int n, m;
cin >> n >> m;
map < pair < int, int >, int > mp;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
if (u > v) swap(u, v);
mp[{u, v}] = 1;
g[u].pb({v, w});
g[v].pb({u, w});
}
int atom;
cin >> atom;
for (int i = 1;i <= n; i++) {
dist[i] = INF;
}
priority_queue < pair < int, int > > q;
for (int i = 1; i <= atom; i++) {
int x;
cin >> x;
q.push({0, x});
// used[x] = true;
dist[x] = 0;
}
while (q.size()) {
int a = q.top().S; q.pop();
if (used[a]) continue;
used[a] = true;
for (auto v : g[a]) {
int b = v.F, w = v.S;
if (dist[a] + w < dist[b]) {
dist[b] = dist[a] + w;
q.push({-dist[b], b});
}
}
}
// for (int i = 1; i <= n; i++) {
// cout << dist[i] << ' ';
// }
//8 3 6 0 8 5 0 7 12
int z;
cin >> z;
while (z--) {
int s, t;
cin >> s >> t;
if (s > t) swap(s, t);
if (mp[{s, t}] == 1) {
cout << min(dist[s], dist[t]) << '\n';
continue;
}
// cout << min(dist[s], dist[t]) << '\n';
int l = 0, r = 500000000;
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
bool check[N];
for (int i = 1; i <= n; i++) {
check[i] = false;
}
queue < int > o;
if (dist[s] >= mid) {
o.push(s);
check[s] = true;
}
while (o.size()) {
int v = o.front(); o.pop();
for (auto to : g[v]) {
if (dist[to.F] >= mid && check[to.F] == false) {
check[to.F] = true;
o.push(to.F);
}
}
}
if (check[t] == true) {
l = mid + 1;
ans = mid;
} else {
r = mid - 1;
}
}
cout << ans << '\n';
}
}
int main() {
// freopen("gates.in", "r", stdin);
// freopen("gates.out", "w", stdout);
Fast
int tc = 1;
// cin >> tc;
while (tc--) {
solve();
}
}
# | 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... |