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;
using ll = long long;
using ld = long double;
using T = tuple<ll, ll, ll>;
#define int long long
#define Base 41
#define sz(a) (int)a.size()
#define FOR(i, a, b) for ( int i = a ; i <= b ; i++ )
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)
#define all(x) x.begin() , x.end()
#define pii pair<int , int>
#define fi first
#define se second
#define Lg(x) 31 - __builtin_clz(x)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int MAX = 5e5 + 5;
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void setupIO(){
#define name "Whisper"
//Phu Trong from Nguyen Tat Thanh High School for gifted student
srand(time(NULL));
cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
cout << fixed << setprecision(10);
}
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
int N, M, K, Q;
vector<pii> adj[MAX];
int g[MAX];
int f[MAX];
struct Dsu{
int n;
vector<int> par, sz;
Dsu(int _n){
this -> n = _n;
par.resize(n + 5);
sz.resize(n + 5, 1);
iota(par.begin(), par.end(), 0ll);
}
int Find(int u){
return u == par[u] ? u : par[u] = Find(par[u]);
}
bool unite(int u, int v){
u = Find(u), v = Find(v);
if (u == v) return 0;
if (sz[u] < sz[v]) swap(u, v);
sz[u] += sz[v];
par[v] = u;
return 1;
}
};
struct Data{
int u, v, w;
bool operator < (const Data& oth) const{
return w > oth.w;
}
};
int up[MAX][21], dep[MAX], mn[MAX][21];
void dfs(int u, int p){
for (auto&[v, w] : adj[u]) if(v ^ p){
up[v][0] = u;
mn[v][0] = w;
dep[v] = dep[u] + 1;
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][i - 1]][i - 1]);
}
dfs(v, u);
}
}
int query(int u, int v){
int ans = INF;
if (dep[u] < dep[v]) swap(u, v);
int dis = dep[u] - dep[v];
for (int i = 0; MASK(i) <= dis; ++i){
if (BIT(dis, i)) minimize(ans, mn[u][i]), u = up[u][i];
}
if (u == v) return ans;
int k = Lg(dep[v]);
for (int i = k; i >= 0; --i) if(up[u][i] != up[v][i]){
minimize(ans, mn[u][i]);
minimize(ans, mn[v][i]);
u = up[u][i], v = up[v][i];
}
minimize(ans, mn[u][0]); minimize(ans, mn[v][0]);
return ans;
}
void Whisper(){
cin >> N >> M;
for (int i = 1; i <= M; ++i){
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
cin >> K;
priority_queue<pii, vector<pii>, greater<pii>> q;
memset(f, 0x3f, sizeof f);
for (int i = 1; i <= K; ++i){
cin >> g[i];
q.push({0, g[i]});
f[g[i]] = 0;
}
vector<int> vis(N + 1);
while (!q.empty()){
auto[du, u] = q.top(); q.pop();
if (vis[u]) continue; vis[u] = 1;
for (auto&[v, w] : adj[u]){
if (minimize(f[v], f[u] + w)){
q.push({f[v], v});
}
}
}
vector<int> ord(N + 1);
iota(ord.begin(), ord.end(), 0);
vector<Data> ed;
for (int i = 1; i <= N; ++i){
int u = ord[i];
for (auto&[v, w] : adj[u]){
int weight = min(f[u], f[v]);
ed.push_back({u, v, weight});
}
}
sort(all(ed));
FOR(i, 0, N) adj[i].clear();
Dsu dsu(N);
for (auto&[u, v, w] : ed){
if (dsu.unite(u, v)){
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
}
memset(mn, 0x3f, sizeof mn);
dfs(1, 0);
cin >> Q;
for (int i = 1; i <= Q; ++i){
int u, v; cin >> u >> v;
cout << query(u, v) << '\n';
}
}
signed main(){
setupIO();
int Test = 1;
// cin >> Test;
for ( int i = 1 ; i <= Test ; i++ ){
Whisper();
if (i < Test) cout << '\n';
}
}
Compilation message (stderr)
plan.cpp: In function 'void Whisper()':
plan.cpp:137:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
137 | if (vis[u]) continue; vis[u] = 1;
| ^~
plan.cpp:137:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
137 | if (vis[u]) continue; vis[u] = 1;
| ^~~
# | 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... |