#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
int n, m;
vector<pair<int, int> > adj[300005];
pair<int, int> edge[300005];
int w[300005], p[300005], fw[300005], bw[300005], in[300005], low[300005];
priority_queue<pair<int, int> ,vector<pair<int, int> >, greater<pair<int, int> > > pq;
bool used[300005], dc[300005];
int T;
vector<int> bridge;
void dfs(int u, int p){
in[u] = low[u] = T++;
if (u == n) dc[u] = true;
for (auto &it : adj[u]){
if (it.ff == p || !used[it.ss]) continue;
if (!in[it.ff]){
dfs(it.ff, u);
low[u] = min(low[u], low[it.ff]);
if (dc[it.ff]){
if (in[it.ff]==low[it.ff]) bridge.push_back(it.ss);
dc[u] = true;
}
} else low[u] = min(low[u], in[it.ff]);
}
}
bool chk(int i){
if (i < fw[n]) return true;
for(int x = 0; x < m; x++)
used[x]=(fw[edge[x].ff] + w[x] + bw[edge[x].ss] <= i ||
fw[edge[x].ss] + w[x] + bw[edge[x].ff] <= i);
T = 1;
bridge.clear();
memset(in, 0, sizeof(in));
memset(dc, false, sizeof(dc));
dfs(1,-1);
for (auto &it : bridge)
if (fw[edge[it].ff] + w[it] + p[it] + bw[edge[it].ss] > i &&
fw[edge[it].ss] + w[it] + p[it] + bw[edge[it].ff] > i) return true;
return false;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for(int x = 0; x < m; x++){
cin >> edge[x].ff >> edge[x].ss >> w[x];
adj[edge[x].ff].push_back({edge[x].ss, x});
adj[edge[x].ss].push_back({edge[x].ff,x});
}
for(int x = m - 1; x > -1; x--) p[x] = max(p[x + 1], w[x + 1]);
memset(fw, 63, sizeof(fw));
fw[1] = 0;
pq.push({fw[1], 1});
while (!pq.empty()){
int node = pq.top().ss, weight = pq.top().ff;
pq.pop();
if (fw[node]!=weight) continue;
for (auto &it : adj[node]){
if (fw[it.ff] > weight + w[it.ss]){
fw[it.ff] = weight + w[it.ss];
if (it.ff != n) pq.push({fw[it.ff], it.ff});
}
}
}
memset(bw, 63, sizeof(bw));
bw[n] = 0;
pq.push({bw[n],n});
while (!pq.empty()){
int node = pq.top().ss, weight=pq.top().ff;
pq.pop();
if (bw[node] != weight) continue;
for (auto &it : adj[node]){
if (bw[it.ff] > weight + w[it.ss]){
bw[it.ff] = weight + w[it.ss];
if (it.ff != 1) pq.push({bw[it.ff], it.ff});
}
}
}
int lo = fw[n] - 1, hi = fw[n] + 1e9 + 10, mid;
while (hi - lo > 1){
mid = (hi + lo) / 2;
if (chk(mid)) lo = mid;
else hi = mid;
}
cout << hi << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14684 KB |
Output is correct |
2 |
Correct |
8 ms |
14816 KB |
Output is correct |
3 |
Correct |
8 ms |
14684 KB |
Output is correct |
4 |
Correct |
7 ms |
14884 KB |
Output is correct |
5 |
Correct |
8 ms |
14760 KB |
Output is correct |
6 |
Correct |
8 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
8 ms |
14640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14684 KB |
Output is correct |
2 |
Correct |
8 ms |
14816 KB |
Output is correct |
3 |
Correct |
8 ms |
14684 KB |
Output is correct |
4 |
Correct |
7 ms |
14884 KB |
Output is correct |
5 |
Correct |
8 ms |
14760 KB |
Output is correct |
6 |
Correct |
8 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
8 ms |
14640 KB |
Output is correct |
9 |
Correct |
9 ms |
14940 KB |
Output is correct |
10 |
Correct |
10 ms |
15120 KB |
Output is correct |
11 |
Correct |
9 ms |
14940 KB |
Output is correct |
12 |
Correct |
10 ms |
15060 KB |
Output is correct |
13 |
Correct |
10 ms |
14936 KB |
Output is correct |
14 |
Correct |
10 ms |
15056 KB |
Output is correct |
15 |
Correct |
9 ms |
14936 KB |
Output is correct |
16 |
Correct |
10 ms |
14936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
48980 KB |
Output is correct |
2 |
Correct |
323 ms |
48844 KB |
Output is correct |
3 |
Correct |
273 ms |
48936 KB |
Output is correct |
4 |
Correct |
256 ms |
49084 KB |
Output is correct |
5 |
Correct |
257 ms |
49232 KB |
Output is correct |
6 |
Correct |
285 ms |
50000 KB |
Output is correct |
7 |
Correct |
276 ms |
49492 KB |
Output is correct |
8 |
Correct |
267 ms |
50004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
50000 KB |
Output is correct |
2 |
Correct |
290 ms |
49492 KB |
Output is correct |
3 |
Correct |
269 ms |
48080 KB |
Output is correct |
4 |
Correct |
270 ms |
48208 KB |
Output is correct |
5 |
Correct |
270 ms |
49232 KB |
Output is correct |
6 |
Correct |
325 ms |
48980 KB |
Output is correct |
7 |
Correct |
268 ms |
49236 KB |
Output is correct |
8 |
Correct |
282 ms |
49680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1072 ms |
53444 KB |
Output is correct |
2 |
Correct |
479 ms |
44880 KB |
Output is correct |
3 |
Correct |
475 ms |
49780 KB |
Output is correct |
4 |
Correct |
472 ms |
49748 KB |
Output is correct |
5 |
Correct |
454 ms |
49748 KB |
Output is correct |
6 |
Correct |
490 ms |
49852 KB |
Output is correct |
7 |
Correct |
446 ms |
49760 KB |
Output is correct |
8 |
Correct |
482 ms |
50236 KB |
Output is correct |
9 |
Correct |
471 ms |
49600 KB |
Output is correct |
10 |
Correct |
536 ms |
50180 KB |
Output is correct |
11 |
Correct |
477 ms |
50004 KB |
Output is correct |
12 |
Correct |
1086 ms |
54216 KB |
Output is correct |
13 |
Correct |
532 ms |
49804 KB |
Output is correct |
14 |
Correct |
237 ms |
59596 KB |
Output is correct |
15 |
Correct |
257 ms |
59700 KB |
Output is correct |
16 |
Correct |
1004 ms |
54472 KB |
Output is correct |
17 |
Correct |
900 ms |
53444 KB |
Output is correct |
18 |
Correct |
926 ms |
54212 KB |
Output is correct |
19 |
Correct |
481 ms |
44820 KB |
Output is correct |
20 |
Correct |
510 ms |
44936 KB |
Output is correct |
21 |
Correct |
506 ms |
44884 KB |
Output is correct |
22 |
Correct |
469 ms |
45028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1072 ms |
53444 KB |
Output is correct |
2 |
Correct |
479 ms |
44880 KB |
Output is correct |
3 |
Correct |
475 ms |
49780 KB |
Output is correct |
4 |
Correct |
472 ms |
49748 KB |
Output is correct |
5 |
Correct |
454 ms |
49748 KB |
Output is correct |
6 |
Correct |
490 ms |
49852 KB |
Output is correct |
7 |
Correct |
446 ms |
49760 KB |
Output is correct |
8 |
Correct |
482 ms |
50236 KB |
Output is correct |
9 |
Correct |
471 ms |
49600 KB |
Output is correct |
10 |
Correct |
536 ms |
50180 KB |
Output is correct |
11 |
Correct |
477 ms |
50004 KB |
Output is correct |
12 |
Correct |
1086 ms |
54216 KB |
Output is correct |
13 |
Correct |
532 ms |
49804 KB |
Output is correct |
14 |
Correct |
237 ms |
59596 KB |
Output is correct |
15 |
Correct |
257 ms |
59700 KB |
Output is correct |
16 |
Correct |
1004 ms |
54472 KB |
Output is correct |
17 |
Correct |
900 ms |
53444 KB |
Output is correct |
18 |
Correct |
926 ms |
54212 KB |
Output is correct |
19 |
Correct |
481 ms |
44820 KB |
Output is correct |
20 |
Correct |
510 ms |
44936 KB |
Output is correct |
21 |
Correct |
506 ms |
44884 KB |
Output is correct |
22 |
Correct |
469 ms |
45028 KB |
Output is correct |
23 |
Correct |
946 ms |
54684 KB |
Output is correct |
24 |
Correct |
462 ms |
44912 KB |
Output is correct |
25 |
Correct |
418 ms |
49488 KB |
Output is correct |
26 |
Correct |
434 ms |
49744 KB |
Output is correct |
27 |
Correct |
442 ms |
49812 KB |
Output is correct |
28 |
Correct |
446 ms |
49860 KB |
Output is correct |
29 |
Correct |
439 ms |
49960 KB |
Output is correct |
30 |
Correct |
441 ms |
49744 KB |
Output is correct |
31 |
Correct |
490 ms |
50256 KB |
Output is correct |
32 |
Correct |
445 ms |
49624 KB |
Output is correct |
33 |
Correct |
472 ms |
50004 KB |
Output is correct |
34 |
Correct |
924 ms |
54460 KB |
Output is correct |
35 |
Correct |
479 ms |
50048 KB |
Output is correct |
36 |
Correct |
207 ms |
58244 KB |
Output is correct |
37 |
Correct |
214 ms |
59064 KB |
Output is correct |
38 |
Correct |
974 ms |
54008 KB |
Output is correct |
39 |
Correct |
912 ms |
53948 KB |
Output is correct |
40 |
Correct |
1053 ms |
54536 KB |
Output is correct |
41 |
Correct |
479 ms |
45040 KB |
Output is correct |
42 |
Correct |
490 ms |
44884 KB |
Output is correct |
43 |
Correct |
556 ms |
44884 KB |
Output is correct |
44 |
Correct |
559 ms |
45104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14684 KB |
Output is correct |
2 |
Correct |
8 ms |
14816 KB |
Output is correct |
3 |
Correct |
8 ms |
14684 KB |
Output is correct |
4 |
Correct |
7 ms |
14884 KB |
Output is correct |
5 |
Correct |
8 ms |
14760 KB |
Output is correct |
6 |
Correct |
8 ms |
14684 KB |
Output is correct |
7 |
Correct |
10 ms |
14684 KB |
Output is correct |
8 |
Correct |
8 ms |
14640 KB |
Output is correct |
9 |
Correct |
9 ms |
14940 KB |
Output is correct |
10 |
Correct |
10 ms |
15120 KB |
Output is correct |
11 |
Correct |
9 ms |
14940 KB |
Output is correct |
12 |
Correct |
10 ms |
15060 KB |
Output is correct |
13 |
Correct |
10 ms |
14936 KB |
Output is correct |
14 |
Correct |
10 ms |
15056 KB |
Output is correct |
15 |
Correct |
9 ms |
14936 KB |
Output is correct |
16 |
Correct |
10 ms |
14936 KB |
Output is correct |
17 |
Correct |
279 ms |
48980 KB |
Output is correct |
18 |
Correct |
323 ms |
48844 KB |
Output is correct |
19 |
Correct |
273 ms |
48936 KB |
Output is correct |
20 |
Correct |
256 ms |
49084 KB |
Output is correct |
21 |
Correct |
257 ms |
49232 KB |
Output is correct |
22 |
Correct |
285 ms |
50000 KB |
Output is correct |
23 |
Correct |
276 ms |
49492 KB |
Output is correct |
24 |
Correct |
267 ms |
50004 KB |
Output is correct |
25 |
Correct |
272 ms |
50000 KB |
Output is correct |
26 |
Correct |
290 ms |
49492 KB |
Output is correct |
27 |
Correct |
269 ms |
48080 KB |
Output is correct |
28 |
Correct |
270 ms |
48208 KB |
Output is correct |
29 |
Correct |
270 ms |
49232 KB |
Output is correct |
30 |
Correct |
325 ms |
48980 KB |
Output is correct |
31 |
Correct |
268 ms |
49236 KB |
Output is correct |
32 |
Correct |
282 ms |
49680 KB |
Output is correct |
33 |
Correct |
1072 ms |
53444 KB |
Output is correct |
34 |
Correct |
479 ms |
44880 KB |
Output is correct |
35 |
Correct |
475 ms |
49780 KB |
Output is correct |
36 |
Correct |
472 ms |
49748 KB |
Output is correct |
37 |
Correct |
454 ms |
49748 KB |
Output is correct |
38 |
Correct |
490 ms |
49852 KB |
Output is correct |
39 |
Correct |
446 ms |
49760 KB |
Output is correct |
40 |
Correct |
482 ms |
50236 KB |
Output is correct |
41 |
Correct |
471 ms |
49600 KB |
Output is correct |
42 |
Correct |
536 ms |
50180 KB |
Output is correct |
43 |
Correct |
477 ms |
50004 KB |
Output is correct |
44 |
Correct |
1086 ms |
54216 KB |
Output is correct |
45 |
Correct |
532 ms |
49804 KB |
Output is correct |
46 |
Correct |
237 ms |
59596 KB |
Output is correct |
47 |
Correct |
257 ms |
59700 KB |
Output is correct |
48 |
Correct |
1004 ms |
54472 KB |
Output is correct |
49 |
Correct |
900 ms |
53444 KB |
Output is correct |
50 |
Correct |
926 ms |
54212 KB |
Output is correct |
51 |
Correct |
481 ms |
44820 KB |
Output is correct |
52 |
Correct |
510 ms |
44936 KB |
Output is correct |
53 |
Correct |
506 ms |
44884 KB |
Output is correct |
54 |
Correct |
469 ms |
45028 KB |
Output is correct |
55 |
Correct |
946 ms |
54684 KB |
Output is correct |
56 |
Correct |
462 ms |
44912 KB |
Output is correct |
57 |
Correct |
418 ms |
49488 KB |
Output is correct |
58 |
Correct |
434 ms |
49744 KB |
Output is correct |
59 |
Correct |
442 ms |
49812 KB |
Output is correct |
60 |
Correct |
446 ms |
49860 KB |
Output is correct |
61 |
Correct |
439 ms |
49960 KB |
Output is correct |
62 |
Correct |
441 ms |
49744 KB |
Output is correct |
63 |
Correct |
490 ms |
50256 KB |
Output is correct |
64 |
Correct |
445 ms |
49624 KB |
Output is correct |
65 |
Correct |
472 ms |
50004 KB |
Output is correct |
66 |
Correct |
924 ms |
54460 KB |
Output is correct |
67 |
Correct |
479 ms |
50048 KB |
Output is correct |
68 |
Correct |
207 ms |
58244 KB |
Output is correct |
69 |
Correct |
214 ms |
59064 KB |
Output is correct |
70 |
Correct |
974 ms |
54008 KB |
Output is correct |
71 |
Correct |
912 ms |
53948 KB |
Output is correct |
72 |
Correct |
1053 ms |
54536 KB |
Output is correct |
73 |
Correct |
479 ms |
45040 KB |
Output is correct |
74 |
Correct |
490 ms |
44884 KB |
Output is correct |
75 |
Correct |
556 ms |
44884 KB |
Output is correct |
76 |
Correct |
559 ms |
45104 KB |
Output is correct |
77 |
Correct |
309 ms |
49416 KB |
Output is correct |
78 |
Correct |
252 ms |
47188 KB |
Output is correct |
79 |
Correct |
230 ms |
52084 KB |
Output is correct |
80 |
Correct |
231 ms |
52048 KB |
Output is correct |
81 |
Correct |
229 ms |
52056 KB |
Output is correct |
82 |
Correct |
205 ms |
51792 KB |
Output is correct |
83 |
Correct |
202 ms |
52308 KB |
Output is correct |
84 |
Correct |
209 ms |
52564 KB |
Output is correct |
85 |
Correct |
210 ms |
52048 KB |
Output is correct |
86 |
Correct |
210 ms |
52052 KB |
Output is correct |
87 |
Correct |
238 ms |
52560 KB |
Output is correct |
88 |
Correct |
269 ms |
48376 KB |
Output is correct |
89 |
Correct |
209 ms |
52304 KB |
Output is correct |
90 |
Correct |
222 ms |
59176 KB |
Output is correct |
91 |
Correct |
210 ms |
59720 KB |
Output is correct |
92 |
Correct |
275 ms |
48832 KB |
Output is correct |
93 |
Correct |
291 ms |
48288 KB |
Output is correct |
94 |
Correct |
267 ms |
47892 KB |
Output is correct |
95 |
Correct |
239 ms |
47040 KB |
Output is correct |
96 |
Correct |
251 ms |
47188 KB |
Output is correct |
97 |
Correct |
242 ms |
47188 KB |
Output is correct |
98 |
Correct |
259 ms |
47440 KB |
Output is correct |