This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#define ll long long
#define maxn 200005
#define pii pair<int, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const ll inf = 1LL<<60;
vector<pii> adj[maxn / 2], ord;
vector<int> tree[maxn];
int anc[18][maxn], dep[maxn];
ll mind[maxn];
int par[maxn];
bool done[maxn];
inline bool cmp(const pii & a, const pii & b) {
return a.ss > b.ss;
}
priority_queue<pii, vector<pii>, bool (*) (const pii &, const pii &) > pq(cmp);
int find(int a) {
return a == par[a] ? a : par[a] = find(par[a]);
}
void dfs(int n, int par, int d) {
anc[0][n] = par;
dep[n] = d;
for (int v:tree[n]) {
if (v != par) {
dfs(v, n, d + 1);
}
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int hdif = dep[a] - dep[b], cnt = 0;
while (hdif) {
if (hdif & 1) {
a = anc[cnt][a];
}
cnt++, hdif >>= 1;
}
if (a == b) return a;
for (int i = 17;i >= 0;i--) {
if (anc[i][a] != anc[i][b]) {
a = anc[i][a], b = anc[i][b];
}
}
return anc[0][a];
}
int main() {
io
int n, m;
cin >> n >> m;
for (int i = 1;i < maxn;i++) mind[i] = inf, par[i] = i;
for (int i = 0;i < m;i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back(make_pair(b, w));
adj[b].push_back(make_pair(a, w));
}
int k;
cin >> k;
for (int i = 0;i < k;i++) {
int gi;
cin >> gi;
mind[gi] = 0;
pq.push(make_pair(gi, 0));
}
while (pq.size()) {
int cur = pq.top().ff;
ll dis = pq.top().ss;
pq.pop();
if (dis != mind[cur]) continue;
for (auto v:adj[cur]) {
ll nd = v.ss + mind[cur];
if (nd < mind[v.ff]) {
mind[v.ff] = nd;
pq.push(make_pair(v.ff, nd));
}
}
}
for (int i = 1;i <= n;i++) {
ord.push_back(make_pair(i, mind[i]));
//cout << mind[i] << " ";
}
//cout << endl;
sort(ord.begin(), ord.end(), cmp);
int cur = n + 1;
for (int i = 0;i < n;i++) {
for (auto v:adj[ord[i].ff]) {
if (!done[v.ff]) continue;
int a = find(ord[i].ff), b = find(v.ff);
if (a != b) {
//cout << ord[i].ff << " " << v.ff << endl;
mind[cur] = min(mind[a], mind[b]);
tree[cur].push_back(a);
tree[a].push_back(cur);
tree[cur].push_back(b);
tree[b].push_back(cur);
par[a] = cur, par[b] = cur;
cur++;
}
}
done[ord[i].ff] = true;
}
dfs(cur - 1, 0, 0);
for (int i = 1;i < 18;i++) {
for (int j = 1;j < cur;j++) anc[i][j] = anc[i - 1][anc[i - 1][j]];
}
int q;
cin >> q;
for (int i = 0;i < q;i++) {
int a, b;
cin >> a >> b;
cout << mind[lca(a, b)] << "\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
*/
# | 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... |