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 fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
return l + abs((ll) rng()) % (r - l + 1);
}
const ll oo = (ll) 1e17;
const int inf = (int) 1e9 + 7, mod = (int) 1e9 + 7;
const int mxn = 5e5 + 5, mxm = 3e2 + 5;
const int LOG = 17, BASE = 311;
void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }
int n, m, k, q;
struct Edge {
int u, v, w;
} e[mxn];
int safe[mxn];
vector<pair<int, int>> g[mxn];
int dp[mxn];
struct dsu {
int n;
vector<int> par, sz;
dsu(int n) : n(n) {
par.resize(n+1);
sz.resize(n+1,1);
rep(i, n) par[i] = i;
}
int root(int u) {
return par[u] == u ? u : par[u] = root(par[u]);
}
bool join(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (sz[u] < sz[v])
swap(u, v);
par[v] = u;
sz[u] += sz[v];
return true;
}
};
vector<pair<int, int>> dag[mxn];
int h[mxn];
int par[mxn][LOG + 5];
int minval[mxn][LOG + 5];
void dfs(int u, int p) {
forn(i, 1, LOG) {
par[u][i] = par[ par[u][i-1] ][i-1];
minval[u][i] = min(minval[u][i-1], minval[ par[u][i-1] ][i-1]);
}
for (pair<int, int> i : dag[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
h[v] = h[u] + 1;
par[v][0] = u;
minval[v][0] = w;
dfs(v, u);
}
return;
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
int k = h[u] - h[v];
for (; k > 0; k -= k & -k)
u = par[u][ctz(k)];
if (u == v) return u;
ford(i, lg(h[u]), 0) if (par[u][i] != par[v][i]) {
u = par[u][i]; v = par[v][i];
}
return par[u][0];
}
int getMin(int u, int k) {
int ans = inf;
for (; k > 0; k -= k & -k) {
minimize(ans, minval[u][ctz(k)]);
u = par[u][ctz(k)];
}
return ans;
}
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define TASK "walk"
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n >> m >> k >> q;
rep(i, m) {
int u, v, w;
cin >> u >> v >> w;
e[i] = {u, v, w};
g[u].pb(mp(v, i));
g[v].pb(mp(u, i));
}
rep(i, k) {
int p;
cin >> p;
safe[p] = true;
}
memset(dp, 0x3f, sizeof dp);
priority_queue<pair<ll, int>> pq;
rep(i, n) {
if (safe[i]) {
dp[i] = 0;
pq.push(mp(0, i));
}
}
while (sz(pq)) {
pair<ll, int> top = pq.top(); pq.pop();
int u = top.se; ll dist = -top.fi;
if (dp[u] != dist) continue;
for (pair<int, int> i : g[u]) {
ll d = dist + e[i.second].w;
if (minimize(dp[i.first], d)) {
pq.push(mp(-d, i.first));
}
}
}
struct Info {
int u, v, w;
bool operator < (const Info &o) const {
return w > o.w;
}
};
vector<Info> res;
forn (i, 1, m) {
res.pb({e[i].u, e[i].v, min(dp[e[i].u], dp[e[i].v])});
}
dsu d(n);
sort(all(res));
for (Info &cur : res) {
if (d.join(cur.u, cur.v)) {
dag[cur.u].pb(mp(cur.v, cur.w));
dag[cur.v].pb(mp(cur.u, cur.w));
}
}
dfs(1, 0);
rep(i, q) {
int u, v;
cin >> u >> v;
int P = lca(u, v);
int ans = min(getMin(u, h[u] - h[P]), getMin(v, h[v] - h[P]));
cout << ans << '\n';
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |