#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 1e5 + 5;
const ll inf = 1e18;
ll n, m;
priority_queue<pll, vector<pll>, greater<pll>> gpq;
priority_queue<pll> pq;
vector<pll> edge[maxn];
vector<ll> dist(maxn, inf);
bool visited[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
ll u, v, w; cin >> u >> v >> w;
edge[u].emplace_back(v, w);
edge[v].emplace_back(u, w);
}
ll npp_cnt;
cin >> npp_cnt;
for (int i = 0; i < npp_cnt; i++) {
ll x; cin >> x;
gpq.emplace(dist[x] = 0, x);
}
//dijkstra
while (!gpq.empty()) {
auto [curdist, u] = gpq.top(); gpq.pop();
if (visited[u]) continue;
visited[u] = true;
for (auto [v, todist] : edge[u]) {
if (dist[v] > dist[u] + todist) {
dist[v] = dist[u] + todist;
gpq.emplace(dist[v], v);
}
}
}
ll q;
cin >> q;
while (q--) {
ll s, t; cin >> s >> t;
while (!pq.empty()) pq.pop();
vector<ll> D(n + 5, -1);
memset(visited, 0, sizeof(visited));
D[s] = dist[s];
pq.emplace(D[s], s);
while (!pq.empty()) {
auto [curmax, u] = pq.top(); pq.pop();
if (visited[u]) continue;
visited[u] = 1;
D[u] = curmax;
if (u == t) {
break;
}
for (auto [v, tomax] : edge[u]) {
ll smt = min(dist[v], curmax);
if (D[v] < smt) {
D[v] = smt;
pq.emplace(smt, v);
}
}
}
cout << D[t] << '\n';
}
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... |