#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);
auto check = [&](ll x) {
std::vector<std::vector<To>> new_adj(n + 1);
for (ll i = 0; i < m; ++i) {
auto [u, v, w] = edges[i];
if (sssp1[u] + w + ssspn[v] <= x or sssp1[v] + w + ssspn[u] <= x) {
new_adj[u].push_back({v, w, i});
new_adj[v].push_back({u, w, i});
}
}
std::vector<bool> is_bridge(m), vis(n + 1), enc(n + 1);
enc[n] = true;
std::vector<ll> low(n + 1), tin(n + 1);
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] : new_adj[node]) {
if (ch == par) {
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] + w + worst_road + ssspn[v],
sssp1[v] + w + worst_road + ssspn[u]) > x) {
return true;
}
worst_road = std::max(worst_road, edges[i].w);
}
return false;
};
ll s = sssp1[n], e = 1e10;
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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
235 ms |
45796 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
253 ms |
46564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2080 ms |
90276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2080 ms |
90276 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |