#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<bool> active_edge(m);
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) {
active_edge[i] = true;
}
}
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] : 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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
936 ms |
44224 KB |
Output is correct |
2 |
Correct |
888 ms |
44400 KB |
Output is correct |
3 |
Correct |
874 ms |
43756 KB |
Output is correct |
4 |
Correct |
882 ms |
44004 KB |
Output is correct |
5 |
Correct |
952 ms |
44020 KB |
Output is correct |
6 |
Correct |
926 ms |
45072 KB |
Output is correct |
7 |
Correct |
931 ms |
44888 KB |
Output is correct |
8 |
Correct |
918 ms |
45032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
946 ms |
45032 KB |
Output is correct |
2 |
Correct |
941 ms |
44260 KB |
Output is correct |
3 |
Correct |
842 ms |
44520 KB |
Output is correct |
4 |
Correct |
846 ms |
45036 KB |
Output is correct |
5 |
Correct |
909 ms |
44264 KB |
Output is correct |
6 |
Correct |
890 ms |
44448 KB |
Output is correct |
7 |
Correct |
920 ms |
44156 KB |
Output is correct |
8 |
Correct |
1006 ms |
44608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
55528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2053 ms |
55528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
936 ms |
44224 KB |
Output is correct |
18 |
Correct |
888 ms |
44400 KB |
Output is correct |
19 |
Correct |
874 ms |
43756 KB |
Output is correct |
20 |
Correct |
882 ms |
44004 KB |
Output is correct |
21 |
Correct |
952 ms |
44020 KB |
Output is correct |
22 |
Correct |
926 ms |
45072 KB |
Output is correct |
23 |
Correct |
931 ms |
44888 KB |
Output is correct |
24 |
Correct |
918 ms |
45032 KB |
Output is correct |
25 |
Correct |
946 ms |
45032 KB |
Output is correct |
26 |
Correct |
941 ms |
44260 KB |
Output is correct |
27 |
Correct |
842 ms |
44520 KB |
Output is correct |
28 |
Correct |
846 ms |
45036 KB |
Output is correct |
29 |
Correct |
909 ms |
44264 KB |
Output is correct |
30 |
Correct |
890 ms |
44448 KB |
Output is correct |
31 |
Correct |
920 ms |
44156 KB |
Output is correct |
32 |
Correct |
1006 ms |
44608 KB |
Output is correct |
33 |
Execution timed out |
2053 ms |
55528 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |