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>
typedef long long ll;
struct To {
ll v, w;
ll i;
};
struct Edge {
ll u, v, w;
};
const ll inf = 1e15;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, m;
std::cin >> n >> m;
std::vector<std::vector<To>> adj(n + 1);
std::vector<Edge> edges;
for (ll i = 0, u, v, w; i < m; ++i) {
std::cin >> u >> v >> w;
adj[u].push_back({v, w, i});
adj[v].push_back({u, w, i});
edges.push_back({u, v, w});
}
auto sssp = [&](ll node) {
std::vector<ll> ans(n + 1, inf);
std::vector<bool> vis(n + 1);
std::priority_queue<std::pair<ll, ll>> pq;
ans[node] = 0;
pq.push({0, node});
while (!pq.empty()) {
auto [d, node] = pq.top();
d *= -1;
pq.pop();
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto [ch, w, cur_i] : adj[node]) {
if (ans[ch] > d + w) {
ans[ch] = d + w;
pq.push({-ans[ch], ch});
}
}
}
return ans;
};
auto sssp1 = sssp(1);
auto ssspn = sssp(n);
std::vector<bool> active_edge(m), is_bridge(m), vis(n + 1), enc(n + 1);
std::vector<ll> low(n + 1), tin(n + 1);
auto check = [&](ll x) {
for (ll i = 0; i < m; ++i) {
auto [u, v, w] = edges[i];
active_edge[i] =
sssp1[u] + w + ssspn[v] <= x or sssp1[v] + w + ssspn[u] <= x;
}
vis.assign(n + 1, false);
enc.assign(n + 1, false);
enc[n] = true;
ll timer = 0;
std::function<void(ll, ll)> dfs;
dfs = [&](ll node, ll par) {
if (vis[node]) {
return;
}
vis[node] = true;
tin[node] = timer++;
low[node] = tin[node];
for (auto &[ch, w, i] : adj[node]) {
if (ch == par or !active_edge[i]) {
continue;
}
if (!vis[ch]) {
dfs(ch, node);
enc[node] = enc[node] || enc[ch];
}
is_bridge[i] = low[ch] > tin[node] and enc[ch];
low[node] = std::min(low[node], low[ch]);
}
};
dfs(1, 0);
ll worst_road = edges[m - 1].w;
for (ll i = m - 2; i >= 0; --i) {
auto [u, v, w] = edges[i];
if (is_bridge[i] and
std::min(sssp1[u] + ssspn[v], sssp1[v] + ssspn[u]) + w + worst_road >
x) {
return true;
}
worst_road = std::max(worst_road, edges[i].w);
}
return false;
};
ll s = sssp1[n], e = 1e15;
while (s < e) {
ll m = s + (e - s) / 2;
if (check(m)) {
s = m + 1;
} else {
e = m;
}
}
std::cout << s << '\n';
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |