#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 69, pr = 31;
const int mxn = 3e5 + 5, mxq = 1e5 + 5, sq = 300, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e18 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
// <3;
int n, m;
ll eu[mxn + 1], ev[mxn + 1], ew[mxn + 1], mx[mxn + 1];
vt<pll>adj[mxn + 1];
struct D{
ll dis[mxn + 1];
void go(int s){
for(int i = 1; i <= n; i++){
dis[i] = inf;
}
dis[s] = 0;
priority_queue<pll, vt<pll>, greater<pll>>pq;
pq.push(mpp(dis[s], s));
while(!pq.empty()){
auto [dd, u] = pq.top(); pq.pop();
for(auto [v, w]: adj[u]){
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
pq.push(mpp(dis[v], v));
}
}
}
}
};
D d1, dn;
int low[mxn + 1], num[mxn + 1], tt = 0;
bool contain[mxn + 1];
vt<pii>g[mxn + 1];
bool ok = 0;
void dfs(int s, int pre, ll mid){
if(s == n)contain[s] = 1;
low[s] = num[s] = ++tt;
for(auto [v, id]: g[s]){
if(id == pre){
continue;
}
if(!num[v]){
dfs(v, id, mid); contain[s] |= contain[v];
//cout << low[v] << " " << num[v] << " ";
low[s] = min(low[s], low[v]);
if(low[v] == num[v] && contain[v]){
if(d1.dis[eu[id]] + dn.dis[ev[id]] + ew[id] == d1.dis[n] && d1.dis[eu[id]] + dn.dis[ev[id]] + ew[id] + mx[id] >= d1.dis[n] + mid){
ok = 1;
//cout << eu[id] << " " << ev[id] << " " << ew[id] << " " << mx[id] << "\n";
}
}
}else{
low[s] = min(low[s], num[v]);
}
}
}
bool ck(ll mid){
for(int i = 1; i <= n; i++){
g[i].clear(); low[i] = num[i] = 0; contain[i] = 0;
}
tt = 0; ok = 0;
for(int i = 0; i < m; i++){
ll u = eu[i], v = ev[i], w = ew[i];
if(d1.dis[u] + dn.dis[v] + ew[i] < d1.dis[n] + mid){
g[u].pb(mpp(v, i)); g[v].pb(mpp(u, i));
}else if(d1.dis[v] + dn.dis[u] + ew[i] < d1.dis[n] + mid){
g[u].pb(mpp(v, i)); g[v].pb(mpp(u, i));
}
}
dfs(1, -1, mid);
//cout << ok << " ";
return(ok);
}
void solve(){
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> eu[i] >> ev[i] >> ew[i];
adj[eu[i]].pb(mpp(ev[i], ew[i])); adj[ev[i]].pb(mpp(eu[i], ew[i]));
}
for(int i = m - 2; i >= 0; i--){
mx[i] = max(mx[i + 1], ew[i + 1]);
}
d1.go(1); dn.go(n);
for(int i = 0; i < m; i++){
if(d1.dis[eu[i]] > d1.dis[ev[i]])swap(eu[i], ev[i]);
}
ll l = 1, r = *max_element(ew + 1, ew + m + 1), ans = 0;
while(l <= r){
ll mid = (l + r) >> 1;
if(ck(mid)){
ans = mid; l = mid + 1;
}else{
r = mid - 1;
}
}
cout << d1.dis[n] + ans;
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("netw.inp", "r", stdin);
//freopen("netw.out", "w", stdout);
int ttt; ttt = 1;
while(ttt--){
solve();
}
return(0);
}
/*
1
5 7 2
1 1 1
3 1 2
2 2 3
4 2 4
2 4 2
1 5 5
3 5 2
5 3
2 7
3 3 1
1 1 1
1 2 2
1 3 3
3 1
*/
Compilation message
Aesthetic.cpp: In member function 'void D::go(int)':
Aesthetic.cpp:44:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | auto [dd, u] = pq.top(); pq.pop();
| ^
Aesthetic.cpp:45:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
45 | for(auto [v, w]: adj[u]){
| ^
Aesthetic.cpp: In function 'void dfs(int, int, long long int)':
Aesthetic.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for(auto [v, id]: g[s]){
| ^
Aesthetic.cpp: In function 'bool ck(long long int)':
Aesthetic.cpp:88:34: warning: unused variable 'w' [-Wunused-variable]
88 | ll u = eu[i], v = ev[i], w = ew[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
9 ms |
29380 KB |
Output is correct |
3 |
Correct |
5 ms |
29272 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
5 ms |
29272 KB |
Output is correct |
8 |
Correct |
5 ms |
29272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
9 ms |
29380 KB |
Output is correct |
3 |
Correct |
5 ms |
29272 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
5 ms |
29272 KB |
Output is correct |
8 |
Correct |
5 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
8 ms |
29532 KB |
Output is correct |
11 |
Correct |
7 ms |
29360 KB |
Output is correct |
12 |
Correct |
7 ms |
29448 KB |
Output is correct |
13 |
Correct |
6 ms |
29532 KB |
Output is correct |
14 |
Correct |
7 ms |
29532 KB |
Output is correct |
15 |
Correct |
7 ms |
29456 KB |
Output is correct |
16 |
Correct |
7 ms |
29532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
54292 KB |
Output is correct |
2 |
Correct |
497 ms |
54096 KB |
Output is correct |
3 |
Correct |
378 ms |
53784 KB |
Output is correct |
4 |
Correct |
396 ms |
54096 KB |
Output is correct |
5 |
Correct |
445 ms |
53920 KB |
Output is correct |
6 |
Correct |
482 ms |
54712 KB |
Output is correct |
7 |
Correct |
475 ms |
54432 KB |
Output is correct |
8 |
Correct |
463 ms |
54528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
515 ms |
54684 KB |
Output is correct |
2 |
Correct |
463 ms |
54056 KB |
Output is correct |
3 |
Correct |
444 ms |
54108 KB |
Output is correct |
4 |
Correct |
398 ms |
54568 KB |
Output is correct |
5 |
Correct |
463 ms |
53996 KB |
Output is correct |
6 |
Correct |
389 ms |
54436 KB |
Output is correct |
7 |
Correct |
384 ms |
54172 KB |
Output is correct |
8 |
Correct |
417 ms |
54356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
53048 KB |
Output is correct |
2 |
Correct |
143 ms |
49504 KB |
Output is correct |
3 |
Correct |
192 ms |
65552 KB |
Output is correct |
4 |
Correct |
208 ms |
65616 KB |
Output is correct |
5 |
Correct |
209 ms |
66316 KB |
Output is correct |
6 |
Correct |
192 ms |
65996 KB |
Output is correct |
7 |
Correct |
226 ms |
65760 KB |
Output is correct |
8 |
Correct |
206 ms |
65860 KB |
Output is correct |
9 |
Correct |
187 ms |
65620 KB |
Output is correct |
10 |
Correct |
183 ms |
66132 KB |
Output is correct |
11 |
Correct |
204 ms |
65948 KB |
Output is correct |
12 |
Correct |
290 ms |
53576 KB |
Output is correct |
13 |
Correct |
191 ms |
65916 KB |
Output is correct |
14 |
Correct |
96 ms |
63600 KB |
Output is correct |
15 |
Correct |
107 ms |
63572 KB |
Output is correct |
16 |
Correct |
233 ms |
53696 KB |
Output is correct |
17 |
Correct |
229 ms |
53020 KB |
Output is correct |
18 |
Correct |
230 ms |
53320 KB |
Output is correct |
19 |
Correct |
120 ms |
49488 KB |
Output is correct |
20 |
Correct |
120 ms |
49540 KB |
Output is correct |
21 |
Correct |
123 ms |
49488 KB |
Output is correct |
22 |
Correct |
122 ms |
49528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
293 ms |
53048 KB |
Output is correct |
2 |
Correct |
143 ms |
49504 KB |
Output is correct |
3 |
Correct |
192 ms |
65552 KB |
Output is correct |
4 |
Correct |
208 ms |
65616 KB |
Output is correct |
5 |
Correct |
209 ms |
66316 KB |
Output is correct |
6 |
Correct |
192 ms |
65996 KB |
Output is correct |
7 |
Correct |
226 ms |
65760 KB |
Output is correct |
8 |
Correct |
206 ms |
65860 KB |
Output is correct |
9 |
Correct |
187 ms |
65620 KB |
Output is correct |
10 |
Correct |
183 ms |
66132 KB |
Output is correct |
11 |
Correct |
204 ms |
65948 KB |
Output is correct |
12 |
Correct |
290 ms |
53576 KB |
Output is correct |
13 |
Correct |
191 ms |
65916 KB |
Output is correct |
14 |
Correct |
96 ms |
63600 KB |
Output is correct |
15 |
Correct |
107 ms |
63572 KB |
Output is correct |
16 |
Correct |
233 ms |
53696 KB |
Output is correct |
17 |
Correct |
229 ms |
53020 KB |
Output is correct |
18 |
Correct |
230 ms |
53320 KB |
Output is correct |
19 |
Correct |
120 ms |
49488 KB |
Output is correct |
20 |
Correct |
120 ms |
49540 KB |
Output is correct |
21 |
Correct |
123 ms |
49488 KB |
Output is correct |
22 |
Correct |
122 ms |
49528 KB |
Output is correct |
23 |
Correct |
313 ms |
54840 KB |
Output is correct |
24 |
Correct |
166 ms |
51536 KB |
Output is correct |
25 |
Correct |
161 ms |
52844 KB |
Output is correct |
26 |
Correct |
150 ms |
51752 KB |
Output is correct |
27 |
Correct |
153 ms |
51980 KB |
Output is correct |
28 |
Correct |
163 ms |
53256 KB |
Output is correct |
29 |
Correct |
165 ms |
51824 KB |
Output is correct |
30 |
Correct |
156 ms |
52188 KB |
Output is correct |
31 |
Correct |
168 ms |
52332 KB |
Output is correct |
32 |
Correct |
157 ms |
51984 KB |
Output is correct |
33 |
Correct |
201 ms |
52612 KB |
Output is correct |
34 |
Correct |
310 ms |
54688 KB |
Output is correct |
35 |
Correct |
175 ms |
52316 KB |
Output is correct |
36 |
Correct |
129 ms |
68548 KB |
Output is correct |
37 |
Correct |
134 ms |
69896 KB |
Output is correct |
38 |
Correct |
266 ms |
53456 KB |
Output is correct |
39 |
Correct |
312 ms |
54540 KB |
Output is correct |
40 |
Correct |
277 ms |
55904 KB |
Output is correct |
41 |
Correct |
166 ms |
51620 KB |
Output is correct |
42 |
Correct |
167 ms |
51612 KB |
Output is correct |
43 |
Correct |
194 ms |
51588 KB |
Output is correct |
44 |
Correct |
165 ms |
51540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29272 KB |
Output is correct |
2 |
Correct |
9 ms |
29380 KB |
Output is correct |
3 |
Correct |
5 ms |
29272 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
6 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
5 ms |
29272 KB |
Output is correct |
8 |
Correct |
5 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
8 ms |
29532 KB |
Output is correct |
11 |
Correct |
7 ms |
29360 KB |
Output is correct |
12 |
Correct |
7 ms |
29448 KB |
Output is correct |
13 |
Correct |
6 ms |
29532 KB |
Output is correct |
14 |
Correct |
7 ms |
29532 KB |
Output is correct |
15 |
Correct |
7 ms |
29456 KB |
Output is correct |
16 |
Correct |
7 ms |
29532 KB |
Output is correct |
17 |
Correct |
431 ms |
54292 KB |
Output is correct |
18 |
Correct |
497 ms |
54096 KB |
Output is correct |
19 |
Correct |
378 ms |
53784 KB |
Output is correct |
20 |
Correct |
396 ms |
54096 KB |
Output is correct |
21 |
Correct |
445 ms |
53920 KB |
Output is correct |
22 |
Correct |
482 ms |
54712 KB |
Output is correct |
23 |
Correct |
475 ms |
54432 KB |
Output is correct |
24 |
Correct |
463 ms |
54528 KB |
Output is correct |
25 |
Correct |
515 ms |
54684 KB |
Output is correct |
26 |
Correct |
463 ms |
54056 KB |
Output is correct |
27 |
Correct |
444 ms |
54108 KB |
Output is correct |
28 |
Correct |
398 ms |
54568 KB |
Output is correct |
29 |
Correct |
463 ms |
53996 KB |
Output is correct |
30 |
Correct |
389 ms |
54436 KB |
Output is correct |
31 |
Correct |
384 ms |
54172 KB |
Output is correct |
32 |
Correct |
417 ms |
54356 KB |
Output is correct |
33 |
Correct |
293 ms |
53048 KB |
Output is correct |
34 |
Correct |
143 ms |
49504 KB |
Output is correct |
35 |
Correct |
192 ms |
65552 KB |
Output is correct |
36 |
Correct |
208 ms |
65616 KB |
Output is correct |
37 |
Correct |
209 ms |
66316 KB |
Output is correct |
38 |
Correct |
192 ms |
65996 KB |
Output is correct |
39 |
Correct |
226 ms |
65760 KB |
Output is correct |
40 |
Correct |
206 ms |
65860 KB |
Output is correct |
41 |
Correct |
187 ms |
65620 KB |
Output is correct |
42 |
Correct |
183 ms |
66132 KB |
Output is correct |
43 |
Correct |
204 ms |
65948 KB |
Output is correct |
44 |
Correct |
290 ms |
53576 KB |
Output is correct |
45 |
Correct |
191 ms |
65916 KB |
Output is correct |
46 |
Correct |
96 ms |
63600 KB |
Output is correct |
47 |
Correct |
107 ms |
63572 KB |
Output is correct |
48 |
Correct |
233 ms |
53696 KB |
Output is correct |
49 |
Correct |
229 ms |
53020 KB |
Output is correct |
50 |
Correct |
230 ms |
53320 KB |
Output is correct |
51 |
Correct |
120 ms |
49488 KB |
Output is correct |
52 |
Correct |
120 ms |
49540 KB |
Output is correct |
53 |
Correct |
123 ms |
49488 KB |
Output is correct |
54 |
Correct |
122 ms |
49528 KB |
Output is correct |
55 |
Correct |
313 ms |
54840 KB |
Output is correct |
56 |
Correct |
166 ms |
51536 KB |
Output is correct |
57 |
Correct |
161 ms |
52844 KB |
Output is correct |
58 |
Correct |
150 ms |
51752 KB |
Output is correct |
59 |
Correct |
153 ms |
51980 KB |
Output is correct |
60 |
Correct |
163 ms |
53256 KB |
Output is correct |
61 |
Correct |
165 ms |
51824 KB |
Output is correct |
62 |
Correct |
156 ms |
52188 KB |
Output is correct |
63 |
Correct |
168 ms |
52332 KB |
Output is correct |
64 |
Correct |
157 ms |
51984 KB |
Output is correct |
65 |
Correct |
201 ms |
52612 KB |
Output is correct |
66 |
Correct |
310 ms |
54688 KB |
Output is correct |
67 |
Correct |
175 ms |
52316 KB |
Output is correct |
68 |
Correct |
129 ms |
68548 KB |
Output is correct |
69 |
Correct |
134 ms |
69896 KB |
Output is correct |
70 |
Correct |
266 ms |
53456 KB |
Output is correct |
71 |
Correct |
312 ms |
54540 KB |
Output is correct |
72 |
Correct |
277 ms |
55904 KB |
Output is correct |
73 |
Correct |
166 ms |
51620 KB |
Output is correct |
74 |
Correct |
167 ms |
51612 KB |
Output is correct |
75 |
Correct |
194 ms |
51588 KB |
Output is correct |
76 |
Correct |
165 ms |
51540 KB |
Output is correct |
77 |
Correct |
400 ms |
57256 KB |
Output is correct |
78 |
Correct |
280 ms |
53932 KB |
Output is correct |
79 |
Correct |
292 ms |
68180 KB |
Output is correct |
80 |
Correct |
343 ms |
67920 KB |
Output is correct |
81 |
Correct |
310 ms |
67936 KB |
Output is correct |
82 |
Correct |
335 ms |
67924 KB |
Output is correct |
83 |
Correct |
311 ms |
68460 KB |
Output is correct |
84 |
Correct |
334 ms |
68432 KB |
Output is correct |
85 |
Correct |
284 ms |
67888 KB |
Output is correct |
86 |
Correct |
300 ms |
68036 KB |
Output is correct |
87 |
Correct |
368 ms |
68432 KB |
Output is correct |
88 |
Correct |
342 ms |
55720 KB |
Output is correct |
89 |
Correct |
307 ms |
68180 KB |
Output is correct |
90 |
Correct |
300 ms |
73232 KB |
Output is correct |
91 |
Correct |
293 ms |
74440 KB |
Output is correct |
92 |
Correct |
365 ms |
57708 KB |
Output is correct |
93 |
Correct |
353 ms |
56872 KB |
Output is correct |
94 |
Correct |
416 ms |
56688 KB |
Output is correct |
95 |
Correct |
273 ms |
54056 KB |
Output is correct |
96 |
Correct |
300 ms |
54116 KB |
Output is correct |
97 |
Correct |
297 ms |
54100 KB |
Output is correct |
98 |
Correct |
284 ms |
54352 KB |
Output is correct |