#include <bits/stdc++.h>
using i64 = long long;
constexpr i64 inf64 = 1ll << 60;
struct Edge {
int to;
i64 cost;
int id;
};
void solve() {
int N, M;
std::cin >> N >> M;
std::vector<int> A(M), B(M);
std::vector<i64> W(M);
std::vector<std::vector<Edge>> graph(N);
for (int i = 0; i < M; ++i) {
std::cin >> A[i] >> B[i] >> W[i];
--A[i], --B[i];
graph[A[i]].push_back({B[i], W[i], i});
graph[B[i]].push_back({A[i], W[i], i});
}
std::vector<i64> C(M);
for (int i = M - 2; i >= 0; --i) {
C[i] = W[i + 1];
C[i] = std::max(C[i], C[i + 1]);
}
auto calcMinDist = [&](const int f) {
std::vector<i64> dist(N, inf64);
std::priority_queue<std::pair<i64, int>, std::vector<std::pair<i64, int>>, std::greater<std::pair<i64, int>>> pq;
dist[f] = 0;
pq.push({0, f});
while (not pq.empty()) {
const auto [nowDist, v] = pq.top();
pq.pop();
if (nowDist > dist[v]) continue;
for (const auto &[t, cost, id] : graph[v]) {
const auto newDist = nowDist + cost;
if (newDist < dist[t]) {
dist[t] = newDist;
pq.push({newDist, t});
}
}
}
return dist;
};
const auto dist1 = calcMinDist(0), dist2 = calcMinDist(N - 1);
const auto minDist = dist1[N - 1];
std::vector<int> edgeType(M);
std::vector<std::vector<Edge>> sGraph(N);
for (int i = 0; i < M; ++i) {
if (dist1[A[i]] > dist1[B[i]]) std::swap(A[i], B[i]);
const auto x = dist1[A[i]], y = dist2[B[i]];
if (x + y + W[i] == minDist) {
edgeType[i] = 1;
sGraph[A[i]].push_back({B[i], W[i], i});
sGraph[B[i]].push_back({A[i], W[i], i});
}
}
std::vector<int> distX(N, -1);
std::queue<int> que;
distX[0] = 0;
que.push(0);
while (not que.empty()) {
const auto f = que.front();
que.pop();
for (const auto &[t, cost, id] : sGraph[f]) {
if (distX[t] == -1) {
distX[t] = distX[f] + 1;
que.push(t);
}
}
}
std::vector<bool> isSeen(N);
std::vector<int> order(N), low(N), bridges;
auto dfsLowlink = [&](auto &&self, const int v, const int p, int k) -> int {
isSeen[v] = true;
order[v] = k++;
low[v] = order[v];
for (const auto &[t, cost, id] : sGraph[v]) {
if (not isSeen[t]) {
k = self(self, t, v, k);
low[v] = std::min(low[v], low[t]);
if (order[v] < low[t]) bridges.push_back(id);
} else if (t != p) {
low[v] = std::min(low[v], order[t]);
}
}
return k;
};
dfsLowlink(dfsLowlink, 0, -1, 0);
std::sort(bridges.begin(), bridges.end(), [&](const int a, const int b) {
const auto x = std::min(dist1[A[a]], dist1[B[a]]), y = std::min(dist1[A[b]], dist1[B[b]]);
if (x != y) return x < y;
const auto x2 = std::min(distX[A[a]], distX[B[a]]), y2 = std::min(distX[A[b]], distX[B[b]]);
return x2 < y2;
});
std::vector<int> edgeId(M, -1);
const int P = (int)bridges.size();
for (int i = 0; i < P; ++i) {
edgeId[bridges[i]] = i;
edgeType[bridges[i]] = 2;
}
std::vector<i64> dist(N, inf64);
std::vector<int> lastR(N, -1);
std::priority_queue<std::pair<i64, int>, std::vector<std::pair<i64, int>>, std::greater<std::pair<i64, int>>> pq;
dist[0] = 0;
pq.push({0, 0});
std::vector<std::vector<i64>> addEvent(P + 1), eraseEvent(P + 1);
while (not pq.empty()) {
const auto [nowDist, v] = pq.top();
pq.pop();
if (nowDist > dist[v]) continue;
assert(nowDist == dist1[v]);
for (const auto &[t, cost, id] : graph[v]) {
const auto newDist = nowDist + cost;
if (dist[t] > newDist) {
dist[t] = newDist;
pq.push({newDist, t});
}
const auto z = edgeId[id];
const auto y = std::max({lastR[t], z, lastR[v]});
if (newDist <= dist[t]) lastR[t] = y;
}
}
auto eval = [&](int a, int b, i64 e, int u) {
// if (dist[a] > dist[b]) return;
const auto x = lastR[a], y = lastR[b];
if (x >= y) return;
addEvent[x + 1].push_back(dist[a] + e + dist2[b]);
eraseEvent[y + 1 - u].push_back(dist[a] + e + dist2[b]);
};
for (int i = 0; i < M; ++i) {
eval(A[i], B[i], W[i], edgeType[i] == 2 ? 1 : 0);
eval(B[i], A[i], W[i], edgeType[i] == 2 ? 1 : 0);
}
std::multiset<i64> s;
std::vector<i64> X(P);
for (int i = 0; i < P; ++i) {
for (const auto v : addEvent[i]) s.insert(v);
for (const auto v : eraseEvent[i]) s.erase(s.find(v));
X[i] = s.empty() ? inf64 : *s.begin();
}
i64 answer = minDist;
for (int i = 0; i < P; ++i) {
const auto x = minDist + C[bridges[i]], y = X[i];
answer = std::max(answer, std::min(x, y));
}
std::cout << answer << std::endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int T = 1;
while (T--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
704 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
535 ms |
61900 KB |
Output is correct |
2 |
Correct |
538 ms |
62264 KB |
Output is correct |
3 |
Correct |
554 ms |
61632 KB |
Output is correct |
4 |
Correct |
527 ms |
61588 KB |
Output is correct |
5 |
Correct |
528 ms |
61928 KB |
Output is correct |
6 |
Correct |
532 ms |
63148 KB |
Output is correct |
7 |
Correct |
544 ms |
62896 KB |
Output is correct |
8 |
Correct |
546 ms |
63232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
539 ms |
63172 KB |
Output is correct |
2 |
Correct |
517 ms |
62216 KB |
Output is correct |
3 |
Correct |
547 ms |
62060 KB |
Output is correct |
4 |
Correct |
595 ms |
63068 KB |
Output is correct |
5 |
Correct |
539 ms |
61892 KB |
Output is correct |
6 |
Correct |
498 ms |
62132 KB |
Output is correct |
7 |
Correct |
553 ms |
62156 KB |
Output is correct |
8 |
Correct |
570 ms |
62592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
55208 KB |
Output is correct |
2 |
Correct |
221 ms |
55840 KB |
Output is correct |
3 |
Correct |
349 ms |
72100 KB |
Output is correct |
4 |
Correct |
406 ms |
72132 KB |
Output is correct |
5 |
Correct |
364 ms |
72144 KB |
Output is correct |
6 |
Correct |
340 ms |
72276 KB |
Output is correct |
7 |
Correct |
402 ms |
72140 KB |
Output is correct |
8 |
Correct |
391 ms |
72464 KB |
Output is correct |
9 |
Correct |
366 ms |
72220 KB |
Output is correct |
10 |
Correct |
353 ms |
72988 KB |
Output is correct |
11 |
Correct |
390 ms |
72380 KB |
Output is correct |
12 |
Correct |
507 ms |
56976 KB |
Output is correct |
13 |
Correct |
359 ms |
72520 KB |
Output is correct |
14 |
Correct |
211 ms |
85040 KB |
Output is correct |
15 |
Correct |
177 ms |
79448 KB |
Output is correct |
16 |
Correct |
416 ms |
55352 KB |
Output is correct |
17 |
Correct |
451 ms |
54152 KB |
Output is correct |
18 |
Correct |
483 ms |
54936 KB |
Output is correct |
19 |
Correct |
214 ms |
55868 KB |
Output is correct |
20 |
Correct |
254 ms |
55944 KB |
Output is correct |
21 |
Correct |
221 ms |
55836 KB |
Output is correct |
22 |
Correct |
211 ms |
55924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
55208 KB |
Output is correct |
2 |
Correct |
221 ms |
55840 KB |
Output is correct |
3 |
Correct |
349 ms |
72100 KB |
Output is correct |
4 |
Correct |
406 ms |
72132 KB |
Output is correct |
5 |
Correct |
364 ms |
72144 KB |
Output is correct |
6 |
Correct |
340 ms |
72276 KB |
Output is correct |
7 |
Correct |
402 ms |
72140 KB |
Output is correct |
8 |
Correct |
391 ms |
72464 KB |
Output is correct |
9 |
Correct |
366 ms |
72220 KB |
Output is correct |
10 |
Correct |
353 ms |
72988 KB |
Output is correct |
11 |
Correct |
390 ms |
72380 KB |
Output is correct |
12 |
Correct |
507 ms |
56976 KB |
Output is correct |
13 |
Correct |
359 ms |
72520 KB |
Output is correct |
14 |
Correct |
211 ms |
85040 KB |
Output is correct |
15 |
Correct |
177 ms |
79448 KB |
Output is correct |
16 |
Correct |
416 ms |
55352 KB |
Output is correct |
17 |
Correct |
451 ms |
54152 KB |
Output is correct |
18 |
Correct |
483 ms |
54936 KB |
Output is correct |
19 |
Correct |
214 ms |
55868 KB |
Output is correct |
20 |
Correct |
254 ms |
55944 KB |
Output is correct |
21 |
Correct |
221 ms |
55836 KB |
Output is correct |
22 |
Correct |
211 ms |
55924 KB |
Output is correct |
23 |
Correct |
572 ms |
58016 KB |
Output is correct |
24 |
Correct |
265 ms |
59468 KB |
Output is correct |
25 |
Correct |
276 ms |
44440 KB |
Output is correct |
26 |
Correct |
302 ms |
47460 KB |
Output is correct |
27 |
Correct |
274 ms |
46632 KB |
Output is correct |
28 |
Correct |
268 ms |
45644 KB |
Output is correct |
29 |
Correct |
304 ms |
47792 KB |
Output is correct |
30 |
Correct |
293 ms |
46092 KB |
Output is correct |
31 |
Correct |
273 ms |
46772 KB |
Output is correct |
32 |
Correct |
298 ms |
46288 KB |
Output is correct |
33 |
Correct |
258 ms |
45908 KB |
Output is correct |
34 |
Correct |
573 ms |
56928 KB |
Output is correct |
35 |
Correct |
291 ms |
47100 KB |
Output is correct |
36 |
Correct |
285 ms |
96104 KB |
Output is correct |
37 |
Correct |
257 ms |
94596 KB |
Output is correct |
38 |
Correct |
575 ms |
57148 KB |
Output is correct |
39 |
Correct |
533 ms |
56840 KB |
Output is correct |
40 |
Correct |
572 ms |
57772 KB |
Output is correct |
41 |
Correct |
274 ms |
59512 KB |
Output is correct |
42 |
Correct |
274 ms |
59452 KB |
Output is correct |
43 |
Correct |
294 ms |
59484 KB |
Output is correct |
44 |
Correct |
277 ms |
59552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
704 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
535 ms |
61900 KB |
Output is correct |
18 |
Correct |
538 ms |
62264 KB |
Output is correct |
19 |
Correct |
554 ms |
61632 KB |
Output is correct |
20 |
Correct |
527 ms |
61588 KB |
Output is correct |
21 |
Correct |
528 ms |
61928 KB |
Output is correct |
22 |
Correct |
532 ms |
63148 KB |
Output is correct |
23 |
Correct |
544 ms |
62896 KB |
Output is correct |
24 |
Correct |
546 ms |
63232 KB |
Output is correct |
25 |
Correct |
539 ms |
63172 KB |
Output is correct |
26 |
Correct |
517 ms |
62216 KB |
Output is correct |
27 |
Correct |
547 ms |
62060 KB |
Output is correct |
28 |
Correct |
595 ms |
63068 KB |
Output is correct |
29 |
Correct |
539 ms |
61892 KB |
Output is correct |
30 |
Correct |
498 ms |
62132 KB |
Output is correct |
31 |
Correct |
553 ms |
62156 KB |
Output is correct |
32 |
Correct |
570 ms |
62592 KB |
Output is correct |
33 |
Correct |
449 ms |
55208 KB |
Output is correct |
34 |
Correct |
221 ms |
55840 KB |
Output is correct |
35 |
Correct |
349 ms |
72100 KB |
Output is correct |
36 |
Correct |
406 ms |
72132 KB |
Output is correct |
37 |
Correct |
364 ms |
72144 KB |
Output is correct |
38 |
Correct |
340 ms |
72276 KB |
Output is correct |
39 |
Correct |
402 ms |
72140 KB |
Output is correct |
40 |
Correct |
391 ms |
72464 KB |
Output is correct |
41 |
Correct |
366 ms |
72220 KB |
Output is correct |
42 |
Correct |
353 ms |
72988 KB |
Output is correct |
43 |
Correct |
390 ms |
72380 KB |
Output is correct |
44 |
Correct |
507 ms |
56976 KB |
Output is correct |
45 |
Correct |
359 ms |
72520 KB |
Output is correct |
46 |
Correct |
211 ms |
85040 KB |
Output is correct |
47 |
Correct |
177 ms |
79448 KB |
Output is correct |
48 |
Correct |
416 ms |
55352 KB |
Output is correct |
49 |
Correct |
451 ms |
54152 KB |
Output is correct |
50 |
Correct |
483 ms |
54936 KB |
Output is correct |
51 |
Correct |
214 ms |
55868 KB |
Output is correct |
52 |
Correct |
254 ms |
55944 KB |
Output is correct |
53 |
Correct |
221 ms |
55836 KB |
Output is correct |
54 |
Correct |
211 ms |
55924 KB |
Output is correct |
55 |
Correct |
572 ms |
58016 KB |
Output is correct |
56 |
Correct |
265 ms |
59468 KB |
Output is correct |
57 |
Correct |
276 ms |
44440 KB |
Output is correct |
58 |
Correct |
302 ms |
47460 KB |
Output is correct |
59 |
Correct |
274 ms |
46632 KB |
Output is correct |
60 |
Correct |
268 ms |
45644 KB |
Output is correct |
61 |
Correct |
304 ms |
47792 KB |
Output is correct |
62 |
Correct |
293 ms |
46092 KB |
Output is correct |
63 |
Correct |
273 ms |
46772 KB |
Output is correct |
64 |
Correct |
298 ms |
46288 KB |
Output is correct |
65 |
Correct |
258 ms |
45908 KB |
Output is correct |
66 |
Correct |
573 ms |
56928 KB |
Output is correct |
67 |
Correct |
291 ms |
47100 KB |
Output is correct |
68 |
Correct |
285 ms |
96104 KB |
Output is correct |
69 |
Correct |
257 ms |
94596 KB |
Output is correct |
70 |
Correct |
575 ms |
57148 KB |
Output is correct |
71 |
Correct |
533 ms |
56840 KB |
Output is correct |
72 |
Correct |
572 ms |
57772 KB |
Output is correct |
73 |
Correct |
274 ms |
59512 KB |
Output is correct |
74 |
Correct |
274 ms |
59452 KB |
Output is correct |
75 |
Correct |
294 ms |
59484 KB |
Output is correct |
76 |
Correct |
277 ms |
59552 KB |
Output is correct |
77 |
Correct |
595 ms |
60092 KB |
Output is correct |
78 |
Correct |
256 ms |
58680 KB |
Output is correct |
79 |
Correct |
388 ms |
49888 KB |
Output is correct |
80 |
Correct |
328 ms |
50056 KB |
Output is correct |
81 |
Correct |
354 ms |
49904 KB |
Output is correct |
82 |
Correct |
340 ms |
49908 KB |
Output is correct |
83 |
Correct |
407 ms |
50420 KB |
Output is correct |
84 |
Correct |
350 ms |
50668 KB |
Output is correct |
85 |
Correct |
335 ms |
50160 KB |
Output is correct |
86 |
Correct |
452 ms |
50340 KB |
Output is correct |
87 |
Correct |
410 ms |
50296 KB |
Output is correct |
88 |
Correct |
640 ms |
58876 KB |
Output is correct |
89 |
Correct |
399 ms |
50776 KB |
Output is correct |
90 |
Correct |
286 ms |
96676 KB |
Output is correct |
91 |
Correct |
287 ms |
95124 KB |
Output is correct |
92 |
Correct |
632 ms |
60168 KB |
Output is correct |
93 |
Correct |
530 ms |
58508 KB |
Output is correct |
94 |
Correct |
560 ms |
57564 KB |
Output is correct |
95 |
Correct |
252 ms |
58832 KB |
Output is correct |
96 |
Correct |
324 ms |
58804 KB |
Output is correct |
97 |
Correct |
255 ms |
58764 KB |
Output is correct |
98 |
Correct |
269 ms |
58648 KB |
Output is correct |