#include <bits/stdc++.h>
//#include <quadmath.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#define sz(x) (int)x.size()
//#define sqr(x) x*x
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("no-stack-protector")
//#pragma GCC optimize("fast-math")
using namespace std;
//using namespace __gnu_pbds;
#define int long long
//#define ld long double
//template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
const int INF = 1e18;
vector<vector<pair<int, int> > > g;
vector<int> es;
vector<int> tin, go;
vector<ll> suff;
vector<bool> used;
vector<ll> d1, dn;
int timer = 0;
bool flag = 0;
void dfs(int v, int p, ll x)
{
used[v] = 1;
tin[v] = go[v] = timer++;
for (auto&[u, ind] : g[v])
{
if (p == u) continue;
if (min(d1[v] + dn[u], d1[u] + dn[v]) + es[ind] >= x) continue;
if (used[u])
{
go[v] = min(go[v], tin[u]);
}
else
{
dfs(u, v, x);
go[v] = min(go[v], go[u]);
if (go[u] > tin[v] && tin.back() >= tin[u] && min(d1[v] + dn[u], d1[u] + dn[v]) + es[ind] + suff[ind + 1] >= x)
{
flag = 1;
}
}
}
}
bool f(ll x)
{
flag = 0;
timer = 0;
fill(tin.begin(), tin.end(), 0);
fill(go.begin(), go.end(), INF);
fill(used.begin(), used.end(), 0);
dfs(0, -1, x);
return flag || tin.back() == 0;
}
void dijkstra(int s, vector<ll>& dist)
{
fill(dist.begin(), dist.end(), 1e18);
set<pair<ll, int> > st;
st.insert({0, s});
dist[s] = 0;
while (!st.empty())
{
auto [d, v] = *st.begin();
st.erase(st.begin());
for (auto&[u, ind] : g[v])
{
ll w = es[ind];
if (dist[u] > dist[v] + w)
{
st.erase({dist[u], u});
dist[u] = dist[v] + w;
st.insert({dist[u], u});
}
}
}
}
void solve()
{
int n, m;
cin >> n >> m;
es.resize(m);
suff.resize(m + 1);
g.resize(n);
tin.resize(n);
d1.resize(n);
dn.resize(n);
go.resize(n);
used.resize(n);
for (int i = 0; i < m; i++)
{
int x, y, z;
cin >> x >> y >> z;
x--; y--;
g[x].push_back({y, i});
g[y].push_back({x, i});
es[i] = z;
}
suff[m - 1] = es[m - 1];
for (int i = m - 2; i >= 0; i--)
{
suff[i] = max(suff[i + 1], es[i]);
}
dijkstra(0, d1);
dijkstra(n - 1, dn);
ll lt = d1[n - 1], rt = d1[n - 1] + suff[0] + 1;
while (rt - lt > 1)
{
ll mid = (lt + rt) >> 1;
if (f(mid))
{
lt = mid;
}
else
{
rt = mid;
}
}
cout << lt;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--)
{
solve();
}
return 0;
}
/*
4 4
1 2 1 3
1 1
1 2
1 3
1 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
556 KB |
Output is correct |
13 |
Correct |
2 ms |
584 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
450 ms |
37244 KB |
Output is correct |
2 |
Correct |
447 ms |
37360 KB |
Output is correct |
3 |
Correct |
469 ms |
36840 KB |
Output is correct |
4 |
Correct |
478 ms |
37004 KB |
Output is correct |
5 |
Correct |
488 ms |
37080 KB |
Output is correct |
6 |
Correct |
503 ms |
37952 KB |
Output is correct |
7 |
Correct |
465 ms |
37672 KB |
Output is correct |
8 |
Correct |
468 ms |
37964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
493 ms |
37932 KB |
Output is correct |
2 |
Correct |
473 ms |
37420 KB |
Output is correct |
3 |
Correct |
437 ms |
37196 KB |
Output is correct |
4 |
Correct |
485 ms |
37840 KB |
Output is correct |
5 |
Correct |
458 ms |
37236 KB |
Output is correct |
6 |
Correct |
435 ms |
37280 KB |
Output is correct |
7 |
Correct |
477 ms |
37320 KB |
Output is correct |
8 |
Correct |
451 ms |
37580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
35148 KB |
Output is correct |
2 |
Correct |
182 ms |
33100 KB |
Output is correct |
3 |
Correct |
221 ms |
32172 KB |
Output is correct |
4 |
Correct |
235 ms |
32316 KB |
Output is correct |
5 |
Correct |
222 ms |
32212 KB |
Output is correct |
6 |
Correct |
223 ms |
32232 KB |
Output is correct |
7 |
Correct |
234 ms |
32492 KB |
Output is correct |
8 |
Correct |
241 ms |
32476 KB |
Output is correct |
9 |
Correct |
219 ms |
32196 KB |
Output is correct |
10 |
Correct |
234 ms |
32588 KB |
Output is correct |
11 |
Correct |
227 ms |
32380 KB |
Output is correct |
12 |
Correct |
436 ms |
35388 KB |
Output is correct |
13 |
Correct |
225 ms |
32348 KB |
Output is correct |
14 |
Correct |
120 ms |
39380 KB |
Output is correct |
15 |
Correct |
118 ms |
39368 KB |
Output is correct |
16 |
Correct |
444 ms |
35276 KB |
Output is correct |
17 |
Correct |
408 ms |
34252 KB |
Output is correct |
18 |
Correct |
425 ms |
34888 KB |
Output is correct |
19 |
Correct |
187 ms |
33228 KB |
Output is correct |
20 |
Correct |
187 ms |
33388 KB |
Output is correct |
21 |
Correct |
185 ms |
33152 KB |
Output is correct |
22 |
Correct |
183 ms |
33220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
446 ms |
35148 KB |
Output is correct |
2 |
Correct |
182 ms |
33100 KB |
Output is correct |
3 |
Correct |
221 ms |
32172 KB |
Output is correct |
4 |
Correct |
235 ms |
32316 KB |
Output is correct |
5 |
Correct |
222 ms |
32212 KB |
Output is correct |
6 |
Correct |
223 ms |
32232 KB |
Output is correct |
7 |
Correct |
234 ms |
32492 KB |
Output is correct |
8 |
Correct |
241 ms |
32476 KB |
Output is correct |
9 |
Correct |
219 ms |
32196 KB |
Output is correct |
10 |
Correct |
234 ms |
32588 KB |
Output is correct |
11 |
Correct |
227 ms |
32380 KB |
Output is correct |
12 |
Correct |
436 ms |
35388 KB |
Output is correct |
13 |
Correct |
225 ms |
32348 KB |
Output is correct |
14 |
Correct |
120 ms |
39380 KB |
Output is correct |
15 |
Correct |
118 ms |
39368 KB |
Output is correct |
16 |
Correct |
444 ms |
35276 KB |
Output is correct |
17 |
Correct |
408 ms |
34252 KB |
Output is correct |
18 |
Correct |
425 ms |
34888 KB |
Output is correct |
19 |
Correct |
187 ms |
33228 KB |
Output is correct |
20 |
Correct |
187 ms |
33388 KB |
Output is correct |
21 |
Correct |
185 ms |
33152 KB |
Output is correct |
22 |
Correct |
183 ms |
33220 KB |
Output is correct |
23 |
Correct |
540 ms |
35492 KB |
Output is correct |
24 |
Correct |
243 ms |
33304 KB |
Output is correct |
25 |
Correct |
218 ms |
27380 KB |
Output is correct |
26 |
Correct |
214 ms |
26700 KB |
Output is correct |
27 |
Correct |
220 ms |
26792 KB |
Output is correct |
28 |
Correct |
232 ms |
27672 KB |
Output is correct |
29 |
Correct |
222 ms |
26860 KB |
Output is correct |
30 |
Correct |
225 ms |
27128 KB |
Output is correct |
31 |
Correct |
224 ms |
27248 KB |
Output is correct |
32 |
Correct |
226 ms |
26924 KB |
Output is correct |
33 |
Correct |
230 ms |
27472 KB |
Output is correct |
34 |
Correct |
515 ms |
35064 KB |
Output is correct |
35 |
Correct |
235 ms |
27196 KB |
Output is correct |
36 |
Correct |
178 ms |
48404 KB |
Output is correct |
37 |
Correct |
200 ms |
48380 KB |
Output is correct |
38 |
Correct |
501 ms |
35020 KB |
Output is correct |
39 |
Correct |
490 ms |
38884 KB |
Output is correct |
40 |
Correct |
522 ms |
39752 KB |
Output is correct |
41 |
Correct |
252 ms |
37860 KB |
Output is correct |
42 |
Correct |
246 ms |
37700 KB |
Output is correct |
43 |
Correct |
249 ms |
37828 KB |
Output is correct |
44 |
Correct |
243 ms |
37836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
556 KB |
Output is correct |
13 |
Correct |
2 ms |
584 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
450 ms |
37244 KB |
Output is correct |
18 |
Correct |
447 ms |
37360 KB |
Output is correct |
19 |
Correct |
469 ms |
36840 KB |
Output is correct |
20 |
Correct |
478 ms |
37004 KB |
Output is correct |
21 |
Correct |
488 ms |
37080 KB |
Output is correct |
22 |
Correct |
503 ms |
37952 KB |
Output is correct |
23 |
Correct |
465 ms |
37672 KB |
Output is correct |
24 |
Correct |
468 ms |
37964 KB |
Output is correct |
25 |
Correct |
493 ms |
37932 KB |
Output is correct |
26 |
Correct |
473 ms |
37420 KB |
Output is correct |
27 |
Correct |
437 ms |
37196 KB |
Output is correct |
28 |
Correct |
485 ms |
37840 KB |
Output is correct |
29 |
Correct |
458 ms |
37236 KB |
Output is correct |
30 |
Correct |
435 ms |
37280 KB |
Output is correct |
31 |
Correct |
477 ms |
37320 KB |
Output is correct |
32 |
Correct |
451 ms |
37580 KB |
Output is correct |
33 |
Correct |
446 ms |
35148 KB |
Output is correct |
34 |
Correct |
182 ms |
33100 KB |
Output is correct |
35 |
Correct |
221 ms |
32172 KB |
Output is correct |
36 |
Correct |
235 ms |
32316 KB |
Output is correct |
37 |
Correct |
222 ms |
32212 KB |
Output is correct |
38 |
Correct |
223 ms |
32232 KB |
Output is correct |
39 |
Correct |
234 ms |
32492 KB |
Output is correct |
40 |
Correct |
241 ms |
32476 KB |
Output is correct |
41 |
Correct |
219 ms |
32196 KB |
Output is correct |
42 |
Correct |
234 ms |
32588 KB |
Output is correct |
43 |
Correct |
227 ms |
32380 KB |
Output is correct |
44 |
Correct |
436 ms |
35388 KB |
Output is correct |
45 |
Correct |
225 ms |
32348 KB |
Output is correct |
46 |
Correct |
120 ms |
39380 KB |
Output is correct |
47 |
Correct |
118 ms |
39368 KB |
Output is correct |
48 |
Correct |
444 ms |
35276 KB |
Output is correct |
49 |
Correct |
408 ms |
34252 KB |
Output is correct |
50 |
Correct |
425 ms |
34888 KB |
Output is correct |
51 |
Correct |
187 ms |
33228 KB |
Output is correct |
52 |
Correct |
187 ms |
33388 KB |
Output is correct |
53 |
Correct |
185 ms |
33152 KB |
Output is correct |
54 |
Correct |
183 ms |
33220 KB |
Output is correct |
55 |
Correct |
540 ms |
35492 KB |
Output is correct |
56 |
Correct |
243 ms |
33304 KB |
Output is correct |
57 |
Correct |
218 ms |
27380 KB |
Output is correct |
58 |
Correct |
214 ms |
26700 KB |
Output is correct |
59 |
Correct |
220 ms |
26792 KB |
Output is correct |
60 |
Correct |
232 ms |
27672 KB |
Output is correct |
61 |
Correct |
222 ms |
26860 KB |
Output is correct |
62 |
Correct |
225 ms |
27128 KB |
Output is correct |
63 |
Correct |
224 ms |
27248 KB |
Output is correct |
64 |
Correct |
226 ms |
26924 KB |
Output is correct |
65 |
Correct |
230 ms |
27472 KB |
Output is correct |
66 |
Correct |
515 ms |
35064 KB |
Output is correct |
67 |
Correct |
235 ms |
27196 KB |
Output is correct |
68 |
Correct |
178 ms |
48404 KB |
Output is correct |
69 |
Correct |
200 ms |
48380 KB |
Output is correct |
70 |
Correct |
501 ms |
35020 KB |
Output is correct |
71 |
Correct |
490 ms |
38884 KB |
Output is correct |
72 |
Correct |
522 ms |
39752 KB |
Output is correct |
73 |
Correct |
252 ms |
37860 KB |
Output is correct |
74 |
Correct |
246 ms |
37700 KB |
Output is correct |
75 |
Correct |
249 ms |
37828 KB |
Output is correct |
76 |
Correct |
243 ms |
37836 KB |
Output is correct |
77 |
Correct |
595 ms |
42596 KB |
Output is correct |
78 |
Correct |
480 ms |
40144 KB |
Output is correct |
79 |
Correct |
347 ms |
38616 KB |
Output is correct |
80 |
Correct |
340 ms |
38604 KB |
Output is correct |
81 |
Correct |
345 ms |
38564 KB |
Output is correct |
82 |
Correct |
325 ms |
38444 KB |
Output is correct |
83 |
Correct |
329 ms |
38960 KB |
Output is correct |
84 |
Correct |
334 ms |
38956 KB |
Output is correct |
85 |
Correct |
339 ms |
38480 KB |
Output is correct |
86 |
Correct |
337 ms |
38628 KB |
Output is correct |
87 |
Correct |
351 ms |
38892 KB |
Output is correct |
88 |
Correct |
584 ms |
41916 KB |
Output is correct |
89 |
Correct |
341 ms |
38852 KB |
Output is correct |
90 |
Correct |
296 ms |
52740 KB |
Output is correct |
91 |
Correct |
311 ms |
53684 KB |
Output is correct |
92 |
Correct |
568 ms |
42188 KB |
Output is correct |
93 |
Correct |
612 ms |
41556 KB |
Output is correct |
94 |
Correct |
556 ms |
41036 KB |
Output is correct |
95 |
Correct |
469 ms |
40056 KB |
Output is correct |
96 |
Correct |
469 ms |
40136 KB |
Output is correct |
97 |
Correct |
483 ms |
40084 KB |
Output is correct |
98 |
Correct |
478 ms |
40140 KB |
Output is correct |