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>
/*
#pragma optimize ("g",on)
#pragma GCC optimize("inline")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
*/
//01001101 01100001 01101011 01101000 01100001 01100111 01100001 01111001
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define pb push_back
#define sz(a) a.size()
#define nl '\n'
#define popb pop_back()
#define ld double
#define ull unsigned long long
#define ff first
#define ss second
#define fix fixed << setprecision
#define pii pair<int, int>
#define E exit (0)
#define int long long
const int inf = 1e18, N = 2e5 + 1, mod = 998244353;
vector<pii> dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pii> adj[N];
vector<int> d(N, inf), tin(N), tout(N);
pii up[N][20];
int p[N], l[N];
int timer;
int get(int x) {
if (p[x] == x) return x;
return p[x] = get(p[x]);
}
void calc(int u, int p = 1, int w = inf) {
tin[u] = timer++;
up[u][0] = {p, w};
for (int i = 1; i < 18; i++) {
up[u][i] = {up[up[u][i - 1].ff][i - 1].ff, min(up[u][i - 1].ss, up[up[u][i - 1].ff][i - 1].ss)};
}
for (auto e: adj[u]) {
if (e.ff != p) {
calc(e.ff, u, min(w, e.ss));
}
}
tout[u] = timer;
}
bool is(int u, int v) {
return (tin[u] <= tin[v] && tout[u] >= tout[v]);
}
int lca(int u, int v) {
if (is(u, v)) return u;
if (is(v, u)) return v;
for (int i = 17; i >= 0; i--) {
int x = up[u][i].ff;
if (is(x, v)) continue;
u = x;
}
return up[u][0].ff;
}
int gett(int u, int v) {
if (u == v) return d[u];
int ans = inf;
for (int i = 17; i >= 0; i--) {
pii x = up[u][i];
if (is(x.ff, v)) continue;
u = x.ff;
ans = min(ans, x.ss);
}
return min(ans, up[u][0].ss);
}
main() {
//freopen("cowrect.in", "r", stdin);
//freopen("cowrect.out", "w", stdout);
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
for (int i = 1; i <= n; i++) p[i] = i, l[i] = 1;
int k;
cin >> k;
set<pii> s;
for (int i = 1; i <= k; i++) {
int a;
cin >> a;
s.insert({0, a});
d[a] = 0;
}
while (!s.empty()) {
int u = (*s.begin()).ss;
s.erase(s.begin());
for (auto e: adj[u]) {
if (d[e.ff] > d[u] + e.ss) {
s.erase({d[e.ff], e.ff});
d[e.ff] = d[u] + e.ss;
s.insert({d[e.ff], e.ff});
}
}
}
vector<pair<int, pii>> g;
for (int i = 1; i <= n; i++) {
for (auto e: adj[i]) {
g.pb({min(d[e.ff], d[i]), {i, e.ff}});
}
adj[i].clear();
}
sort(rall(g));
for (auto e: g) {
int u = get(e.ss.ff), v = get(e.ss.ss);
if (u != v) {
if (l[u] > l[v]) swap(u, v);
p[u] = v;
l[v] += l[u];
adj[e.ss.ff].pb({e.ss.ss, e.ff});
adj[e.ss.ss].pb({e.ss.ff, e.ff});
}
}
calc(1);
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
int lc = lca(u, v);
cout << min(gett(u, lc), gett(v, lc)) << nl;
}
}
Compilation message (stderr)
plan.cpp:91:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main() {
| ^~~~
# | 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... |