This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
typedef long long ll;
const int LG = 17;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int INF = 1e9;
const double PI = acos(-1.0);
struct item {
int w, x, y;
};
vector < item > e;
int n, m, k, q, p[N], u[N], v[N], lvl[N], up[N][LG + 5], mn[N][LG + 5];
ll d[N];
vector < pair < int, int > > g[N], g1[N];
int get(int v) {
return (v == p[v] ? v : p[v] = get(p[v]));
}
int unite(int v, int u) {
v = get(v);
u = get(u);
if (rand() & 1)
swap(v, u);
if (v != u)
p[u] = v;
}
int solve(int s, int t) {
if (lvl[s] < lvl[t])
swap(s, t);
int ans = min(mn[s][0], mn[t][0]);
for (int i = LG; i >= 0; --i) {
if (up[s][i] > 0 && lvl[up[s][i]] >= lvl[t]) {
ans = min(mn[s][i], ans);
s = up[s][i];
}
}
if (s == t)
return ans;
for (int i = LG; i >= 0; --i) {
if (up[s][i] > 0 && up[t][i] > 0 && up[s][i] != up[t][i]) {
ans = min(mn[s][i], ans);
ans = min(mn[t][i], ans);
s = up[s][i];
t = up[t][i];
}
}
return min({ans, mn[s][0], mn[t][0]});
}
bool cmp(item a, item b) {
return a.w < b.w;
}
inline void dfs(int v, int p = 0, int len = INF) {
up[v][0] = p;
mn[v][0] = len;
lvl[v] = lvl[p] + 1;
for (int i = 1; i <= LG; ++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]);
}
for (auto node : g1[v]) {
int to = node.first, lenn = node.second;
if (p == to)
continue;
dfs(to, v, lenn);
}
}
int main() {
srand(time(NULL));
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
d[i] = INF;
p[i] = i;
mn[i][0] = INF;
}
for (int i = 1; i <= m; ++i) {
int w;
cin >> u[i] >> v[i] >> w;
g[v[i]].push_back({u[i], w});
g[u[i]].push_back({v[i], w});
}
cin >> k;
set < pair < int, int > > st;
for (int i = 1, x; i <= k; ++i) {
cin >> x;
d[x] = 0;
st.insert({0, x});
}
while (!st.empty()) {
int x = st.begin() -> second;
st.erase(st.begin());
for (auto node : g[x]) {
int to = node.first, len = node.second;
if (d[to] > d[x] + len) {
st.erase({d[to], to});
d[to] = d[x] + len;
st.insert({d[to], to});
}
}
}
for (int i = 1; i <= n; ++i) {
for (auto node : g[i]) {
int to = node.first;
e.push_back({-min(d[to], d[i]), i, to});
}
}
sort(e.begin(), e.end(), &cmp);
for (auto ver : e) {
int x = ver.x, y = ver.y;
if (get(x) != get(y)) {
g1[x].push_back({y, -ver.w});
g1[y].push_back({x, -ver.w});
unite(x, y);
}
}
dfs(1);
cin >> q;
for (int i = 1; i <= q; ++i) {
int s, t;
cin >> s >> t;
cout << solve(s, t) << "\n";
}
}
Compilation message (stderr)
plan.cpp: In function 'int unite(int, int)':
plan.cpp:39:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
plan.cpp: In function 'int main()':
plan.cpp:120:26: warning: narrowing conversion of '-(long long int)std::min<long long int>(d[to], d[i])' from 'long long int' to 'int' inside { } [-Wnarrowing]
e.push_back({-min(d[to], d[i]), i, to});
^~~~~~~~~~~~~~~~~
# | 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... |