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>
using namespace std;
#define endl '\n'
#define int long long
typedef long long ll;
const int M = 1e5 + 5, MOD = 1e9+7;
vector<pair<int, int>> node[M];
int dist[M];
// sparse
vector<int> ar = {0};
int d[M], up[M][20], mn[M][20], in[M], mxd = 0;
void dfs(int s, int p = 1, int dp = 1, int ed = INT_MAX) {
mxd = max(mxd, dp);
ar.push_back(s);
d[s] = dp++;
up[s][0] = p;
mn[s][0] = ed;
for (auto [i, x]:node[s]) {
if (i != p) {
dfs(i, s, dp, x);
ar.push_back(s);
}
}
}
int seg[8*M], L, R;
int N = exp2(ceil(log2(2*M)));
int comb(int a, int b) {
if (d[ar[a]] < d[ar[b]]) return a;
return b;
}
void update(int ind) {
while (ind /= 2) seg[ind] = comb(seg[ind*2], seg[ind*2+1]);
}
int query(int l = 1, int r = N, int ind = 1) {
if (l > R || r < L) return 0;
if (L <= l && r <= R) return seg[ind];
return comb(query(l, (l+r)/2, ind*2), query((l+r)/2+1, r, ind*2+1));
}
int lca(int a, int b) {
L = in[a], R = in[b];
if (L > R) swap(L, R);
return ar[query()];
}
int solve(int a, int b) {
int ans = INT_MAX;
int x = d[a]-d[b];
int cur = a;
for (int i = 0; i < 31; i++) {
if (x&(1<<i)) {
ans = min(ans, mn[cur][i]);
cur = up[cur][i];
}
} return ans;
}
// dsu
int p[M], rnk[M], cnt;
int find(int a) {
return (a==p[a]?a:p[a]=find(p[a]));
}
void uni(int a, int b) {
if (rnk[a] < rnk[b]) swap(a, b);
if (rnk[a] == rnk[b]) rnk[a]++;
p[b] = a;
cnt--;
}
struct e {int a, b, c;};
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
srand(time(0));
int n, m;
cin >> n >> m;
vector<e> v;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
node[a].push_back({b, c});
node[b].push_back({a, c});
v.push_back({a, b, c});
}
priority_queue<pair<int, int>> q;
int k;
cin >> k;
for (int i = 1; i <= n; i++) dist[i] = INT_MAX;
for (int i = 1; i <= k; i++) {
int a;
cin >> a;
dist[a] = 0;
q.push({0, a});
}
while (!q.empty()) {
auto [dis, nod] = q.top(); q.pop();
if (dis < dist[nod]) continue;
for (auto [x, d]:node[nod]) {
if (d + dis < dist[x]) {
dist[x] = d + dis;
q.push({dist[x], x});
}
}
}
for (auto&[a, b, c]:v) c = min(dist[a], dist[b]);
sort(v.begin(), v.end(), [](e a, e b){return a.c < b.c;});
// for (auto [a, b, c]:v) cout << a << ' ' << b << ' ' << c << endl;
for (int i = 1; i <= n; i++) node[i].clear();
for (int i = 1; i <= n; i++) p[i] = i;
cnt = n;
while (cnt > 1) {
auto [a, b, c] = v.back(); v.pop_back();
if (find(a) != find(b)) {
node[a].push_back({b, c});
node[b].push_back({a, c});
// cout << a << ' ' << b << endl;
uni(find(a), find(b));
}
}
dfs(1);
d[0] = INT_MAX;
for (int i = 1; (1<<i) <= mxd; i++) {
for (int j = 1; j <= n; j++) {
up[j][i] = up[up[j][i-1]][i-1];
mn[j][i] = min(mn[j][i-1], mn[up[j][i-1]][i-1]);
}
}
for (int i = 1; i < ar.size(); i++) {
in[ar[i]] = i;
}
for (int i = 1; i < ar.size(); i++) {
seg[i+N-1] = i;
update(i+N-1);
}
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
int l = lca(a, b);
cout << min(solve(a, l), solve(b, l)) << endl;
}
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
*/
Compilation message (stderr)
plan.cpp: In function 'void dfs(long long int, long long int, long long int, long long int)':
plan.cpp:19:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | for (auto [i, x]:node[s]) {
| ^
plan.cpp: In function 'int main()':
plan.cpp:106:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
106 | auto [dis, nod] = q.top(); q.pop();
| ^
plan.cpp:108:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
108 | for (auto [x, d]:node[nod]) {
| ^
plan.cpp:116:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
116 | for (auto&[a, b, c]:v) c = min(dist[a], dist[b]);
| ^
plan.cpp:126:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
126 | auto [a, b, c] = v.back(); v.pop_back();
| ^
plan.cpp:144:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for (int i = 1; i < ar.size(); i++) {
| ~~^~~~~~~~~~~
plan.cpp:148:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for (int i = 1; i < ar.size(); i++) {
| ~~^~~~~~~~~~~
# | 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... |