#include <bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 2 * 1e5 + 100, M = 500 + 10, len = 315, inf = 1e18;
const ll mod = 998244353;
ll um(ll a, ll b){
return (1LL * a * b) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll bp(ll x, ll step){
ll res = 1;
while(step){
if(step & 1) res = um(res, x);
x = um(x, x);
step /= 2;
}
return res;
}
ll inv(ll x){
return bp(x, mod - 2);
}
int n, m, k, d[N], p[N], sz[N];
vector <pii> q[N], qr[N];
void bfs(int x){
set <pii> st;
d[x] = 0;
st.insert({d[x], x});
while((int)st.size() != 0){
int v = st.begin()->S;
st.erase(st.begin());
for(auto u : q[v]){
if(d[u.F] > d[v] + u.S){
d[u.F] = d[v] + u.S;
st.insert({d[u.F], u.F});
}
}
}
}
int tin[N], tout[N], timer, up[N][25], mn[N][25];
vector <pii> rev[N];
void dfs(int v, int p, int from){
tin[v] = ++timer;
up[v][0] = p;
mn[v][0] = from;
for(int i = 1; i <= 20; i++){
up[v][i] = up[up[v][i - 1]][i - 1];
mn[v][i] = min(mn[v][i - 1], mn[up[v][0]][i - 1]);
}
for(auto u : rev[v]){
if(u.F == p) continue;
dfs(u.F, v, u.S);
}
tout[v] = timer;
}
bool upper(int a, int b){
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca(int a, int b){
if(upper(a, b)) swap(a, b);
int ans = INT_MAX;
if(upper(b, a)){
for(int i = 20; i >= 0; i--){
if(up[a][i] == 0) continue;
if(upper(up[a][i], b) == false){
ans = min(ans, mn[a][i]);
a = up[a][i];
}
}
return min(ans, mn[a][0]);
}
for(int i = 20; i >= 0; i--){
if(up[a][i] == 0) continue;
if(upper(up[a][i], b) == false){
ans = min(ans, mn[a][i]);
a = up[a][i];
}
}
for(int i = 20; i >= 0; i--){
if(up[b][i] == 0) continue;
if(upper(up[b][i], a) == false){
ans = min(ans, mn[b][i]);
b = up[b][i];
}
}
return min(ans, min(mn[a][0], mn[b][0]));
}
int get(int x){
if(x == p[x]) return x;
else return p[x] = get(p[x]);
}
void addthem(ll a, ll b, ll x){
int oa = a, ob = b;
a = get(a);
b = get(b);
if(a != b){
rev[oa].pb({ob, x});
rev[ob].pb({oa, x});
if(sz[b] > sz[a]) swap(a, b);
sz[a] += sz[b];
p[b] = a;
}
}
int main() {
//ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
cin >> n >> m;
for(int i = 0, a, b, c; i < m; i++){
cin >> a >> b >> c;
q[a].pb({b, c});
q[b].pb({a, c});
}
for(int i = 1; i <= n; i++){
d[i] = INT_MAX;
p[i] = i;
sz[i] = 1;
}
cin >> k;
for(int i = 0, x; i < k; i++){
cin >> x;
bfs(x);
}
vector <pair <int, pii>> vec;
for(int i = 1; i <= n; i++){
for(auto u : q[i]){
if(i > u.F) vec.pb({-min(d[u.F], d[i]), {i, u.F}});
}
}
sort(vec.begin(), vec.end());
for(auto u : vec){
addthem(u.S.F, u.S.S, -u.F);
}
dfs(1, 0, INT_MAX);
// for(int i = 1; i <= n; i++){
// for(auto u : rev[i]){
// cout << i << " "<< u.F << " " << -u.S << endl;
// }
// }
int quer;
cin >> quer;
for(int i = 0, a, b; i < quer; i++){
cin >> a >> b;
cout << lca(a, b) << endl;
}
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... |