This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
I find it wholesome to be alone the greater part of the time.
To be in company, even with the best, is soon wearisome and dissipating.
I love to be alone.
I never found the companion that was so companionable as solitude.
*/
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, m, k, q;
vector <pii> adj[maxN];
vector <int> adj1[maxN];
int g[maxN];
ll ans[maxN];
ll d[maxN];
bool on[maxN];
int idx[maxN];
struct TDSU{
vector <int> lab;
TDSU(){}
TDSU(int _n){
lab.resize(_n, -1);
}
inline void resize(int _n){
lab.resize(_n, -1);
}
inline int find_set(int u){
if (lab[u] < 0) return u;
return lab[u] = find_set(lab[u]);
}
inline bool unite(int u, int v){
u = find_set(u);
v = find_set(v);
if (u == v) return 0;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return 1;
}
};
void Dijkstra(){
memset(d, 0x3f, sizeof(d));
priority_queue <pair <ll, int>, vector <pair <ll, int>>, greater <pair <ll, int>>> PQ;
for (int i = 1; i <= k; ++i){
PQ.push({0, g[i]});
d[g[i]] = 0;
}
while (!PQ.empty()){
auto r = PQ.top(); PQ.pop();
if (d[r.se] != r.fi) continue;
for (const pii &i: adj[r.se]){
if (d[r.se] + i.se < d[i.fi]){
d[i.fi] = d[r.se] + i.se;
PQ.push({d[i.fi], i.fi});
}
}
}
}
int lift[maxN][20];
ll val[maxN][20];
int depth[maxN];
void dfs(int u, int p){
depth[u] = depth[p] + 1;
lift[u][0] = p;
val[u][0] = d[u];
for (int i = 1; i < 20; ++i){
lift[u][i] = lift[lift[u][i - 1]][i - 1];
val[u][i] = min(val[u][i - 1], val[lift[u][i - 1]][i - 1]);
}
for (int i: adj1[u]){
if (i == p) continue;
dfs(i, u);
}
}
int lca(int u, int v){
if (depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
ll res = inf;
while (k){
int t = __builtin_ctz(k);
res = min(res, val[u][t]);
u = lift[u][t];
k ^= 1 << t;
}
if (u == v) return min(res, d[u]);
for (int i = 19; i >= 0; --i){
if (lift[u][i] != lift[v][i]){
res = min(res, val[u][i]);
res = min(res, val[v][i]);
u = lift[u][i];
v = lift[v][i];
}
}
return min(res, min(val[v][1], val[u][1]));
}
void Init(){
cin >> n >> m;
TDSU dsu(n + 1);
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});
}
cin >> k;
for (int i = 1; i <= k; ++i) cin >> g[i];
Dijkstra();
iota(idx + 1, idx + n + 1, 1);
sort(idx + 1, idx + n + 1,[&](const int &i, const int &j){
return d[i] > d[j];
});
for (int i = 1; i <= n; ++i){
on[idx[i]] = 1;
for (auto j: adj[idx[i]]){
if (!on[j.fi]) continue;
if (dsu.unite(idx[i], j.fi)){
adj1[idx[i]].pb(j.fi);
adj1[j.fi].pb(idx[i]);
}
}
}
dfs(1, 0);
cin >> q;
for (int i = 1; i <= q; ++i){
int s, t;
cin >> s >> t;
cout << lca(s, t) << "\n";
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen(taskname".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... |