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 <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"
//#define int ll
const int N = 5e5 + 5;
const int LG = 19;
const int oo = 1e9 + 5;
int n, m, k, q, d[N], comp[N];
vector <pii> g[N], p;
vector <int> ng[N];
set <pii> st;
bool can[N];
int par[N][LG+1], mn[N][LG+1], tin[N], tout[N], tiktak;
int get(int x) {
if (comp[x] == x) { return x; }
return comp[x] = get(comp[x]);
}
void dfs(int v, int pr) {
par[v][0] = pr;
mn[v][0] = d[v];
for (int i = 1; i <= LG; ++i) {
par[v][i] = par[par[v][i-1]][i-1];
mn[v][i] = min(mn[v][i-1], mn[par[v][i-1]][i-1]);
}
tin[v] = ++tiktak;
for (int to : ng[v]) {
if (to == pr) { continue; }
dfs(to, v);
}
tout[v] = ++tiktak;
}
bool is_parent(int a, int b) {
return (tin[a] <= tin[b] && tout[b] <= tout[a]);
}
int lca(int a, int b) {
if (is_parent(a, b)) { return a; }
if (is_parent(b, a)) { return b; }
for (int i = LG; i >= 0; --i) {
if (!is_parent(par[a][i], b)) {
a = par[a][i];
}
}
return par[a][0];
}
int Min(int pr, int v) {
int res = mn[v][0];
for (int i = LG; i >= 0; --i) {
if (!is_parent(par[v][i], pr)) {
res = min(res, mn[v][i]);
v = par[v][i];
res = min(res, mn[v][0]);
}
}
res = min(res, mn[pr][0]);
return res;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b, w;
cin >> a >> b >> w;
g[a].pb({b, w});
g[b].pb({a, w});
}
for (int i = 1; i <= n; ++i) { d[i] = oo; }
cin >> k;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
d[x] = 0;
st.insert({0, x});
}
// Dijkstra
while (sz(st)) {
int v = st.begin() -> S;
st.erase(st.begin());
for (pii x : g[v]) {
int to = x.F, w = x.S;
if (d[to] > d[v] + w) {
st.erase({d[to], to});
d[to] = d[v] + w;
st.insert({d[to], to});
}
}
}
//for (int i = 1; i <= n; ++i) { show(i); show(d[i]); nl; } // OK -> check Dijkstra
// New Graph with maximum cost on path -> Vrode OK
for (int i = 1; i <= n; ++i) { p.pb({d[i], i}); comp[i] = i; }
sort(all(p));
for (int i = n; i >= 1; --i) {
int v = p[i-1].S, comp_v = get(v); //show(v); show(d[v]); nl;
for (pii x : g[v]) {
int comp_x = get(x.F);
if (comp_v != comp_x && d[x.F] >= d[v]) {
ng[v].pb(x.F); //cerr << v <<" ";
ng[x.F].pb(v); //cerr << x.F <<"\n";
comp[comp_x] = comp_v;
}
}
}
dfs(1, 1);
cin >> q;
for (int i = 1; i <= q; ++i) {
int a, b;
cin >> a >> b; //show(d[a]); show(d[b]); nl;
int l = lca(a, b);
cout << min(Min(l, a), Min(l, b)) <<"\n";
}
}
main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
//cin >> tests;
while (tests --) {
solve();
}
return 0;
}
/*
Just Chalish!
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:154:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
154 | 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... |