#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)3e5 + 10;
int u[N], v[N], w[N];
int p[N];
vector<pii> T[N];
ll dist[2][N];
bool has[N];
int n,m,c;
void dijik(int x){
priority_queue<pii,vector<pii>,greater<pii>> gg;
int node;
for(int i = 1; i <= n; i ++ ){
dist[c][i]=(ll)1e14;
has[i]=false;
}
dist[c][x]=0ll;
gg.push(mp(0,x));
while(!gg.empty()){
node = gg.top().se;
gg.pop();
if(has[node]) continue;
has[node]=true;
for(auto x : T[node]){
if(dist[c][node] + w[x.se] < dist[c][x.fi]){
dist[c][x.fi] = dist[c][node] + w[x.se];
gg.push(mp(dist[c][x.fi], x.fi));
}
}
}
c++;
}
vector<pii> E[N];
int tin[N];
int low[N];
ll attm;
int tt;
bool bridge[N];
void dfs(int u, int par){
tin[u] = ++tt;
low[u] = tin[u];
for(auto x : E[u]){
if(x.fi == par) continue;
if(tin[x.fi] != -1){
low[u] = min(low[u], tin[x.fi]);
}
else{
dfs(x.fi, u);
low[u] = min(low[u], low[x.fi]);
if(tin[u] < low[x.fi]){
bridge[x.se] = true;
}
}
}
}
int ids[N];
int id;
void dfs1(int u, int par){
ids[u]=id;
for(auto x : E[u]){
if(ids[x.fi] != -1) continue;
if(bridge[x.se]) continue;
dfs1(x.fi, u);
}
}
bool bad;
int pid[N];
int ip[N];
bool vis[N];
void dfs2(int u, int par){
vis[u]=true;
for(auto x : E[u]){
if(vis[x.fi]) continue;
if(bridge[x.se]){
pid[ids[x.fi]] = ids[u];
ip[ids[x.fi]] = x.se;
dfs2(x.fi, u);
}
else{
dfs2(x.fi, u);
}
}
}
bool check(ll dis){
attm = dis;
tt = 0;
for(int i = 0 ; i < m ; i ++ ){
bridge[i]=false;
}
for(int i = 1; i <= n; i ++ ){
E[i].clear();
vis[i]=false;
tin[i] = -1, low[i] = -1;
ids[i] = -1;
pid[i] = -1;
ip[i] = -1;
}
id = 0;
for(int i = 0 ; i < m ; i ++ ){
if(dist[0][u[i]] + w[i] + dist[1][v[i]] <= dis || dist[0][v[i]] + w[i] + dist[1][u[i]] <= dis){
E[u[i]].push_back(mp(v[i], i));
E[v[i]].push_back(mp(u[i], i));
}
}
dfs(1, 0);
if(tin[n] == -1) return false; // we assume it is reachable at this point
bad = false;
for(int i = 1; i <= n; i ++ ){
if(tin[i] != -1 && ids[i] == -1){
id ++ ;
dfs1(i,-1);
}
}
dfs2(1, 0);
int node = ids[n];
int ed;
while(node != ids[1]){
ed = ip[node];
node = pid[node];
if(dist[0][u[ed]] + dist[1][v[ed]] + w[ed] + p[ed+1] > dis && dist[1][u[ed]] + dist[0][v[ed]] + w[ed] + p[ed+1] > dis)
return false;
}
return true;
}
int main(){
fastIO;
cin >> n >> m;
for(int i = 0 ; i < m ; i ++ ){
cin >> u[i] >> v[i] >> w[i];
T[u[i]].push_back(mp(v[i],i));
T[v[i]].push_back(mp(u[i],i));
}
dijik(1);
dijik(n);
for(int i = m-1; i >= 0 ; i -- ){
p[i]=w[i];
p[i]=max(p[i],p[i+1]);
}
ll li = dist[0][n], ri = li+p[1];
ll mid;
while(li < ri){
mid = (li + ri) / 2;
if(check(mid))
ri = mid;
else
li = mid + 1;
}
cout << li << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14592 KB |
Output is correct |
3 |
Correct |
10 ms |
14568 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14592 KB |
Output is correct |
3 |
Correct |
10 ms |
14568 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14712 KB |
Output is correct |
10 |
Correct |
12 ms |
14720 KB |
Output is correct |
11 |
Correct |
16 ms |
14720 KB |
Output is correct |
12 |
Correct |
16 ms |
14720 KB |
Output is correct |
13 |
Correct |
14 ms |
14848 KB |
Output is correct |
14 |
Correct |
14 ms |
14848 KB |
Output is correct |
15 |
Correct |
13 ms |
14848 KB |
Output is correct |
16 |
Correct |
12 ms |
14848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
718 ms |
46484 KB |
Output is correct |
2 |
Correct |
724 ms |
53380 KB |
Output is correct |
3 |
Correct |
770 ms |
52932 KB |
Output is correct |
4 |
Correct |
748 ms |
52852 KB |
Output is correct |
5 |
Correct |
806 ms |
53052 KB |
Output is correct |
6 |
Correct |
790 ms |
54052 KB |
Output is correct |
7 |
Correct |
753 ms |
53696 KB |
Output is correct |
8 |
Correct |
801 ms |
54016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
970 ms |
47252 KB |
Output is correct |
2 |
Correct |
883 ms |
53364 KB |
Output is correct |
3 |
Correct |
749 ms |
53156 KB |
Output is correct |
4 |
Correct |
742 ms |
53924 KB |
Output is correct |
5 |
Correct |
750 ms |
53132 KB |
Output is correct |
6 |
Correct |
738 ms |
53368 KB |
Output is correct |
7 |
Correct |
734 ms |
53276 KB |
Output is correct |
8 |
Correct |
769 ms |
53496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
484 ms |
43144 KB |
Output is correct |
2 |
Correct |
205 ms |
47608 KB |
Output is correct |
3 |
Correct |
474 ms |
63544 KB |
Output is correct |
4 |
Correct |
518 ms |
63540 KB |
Output is correct |
5 |
Correct |
496 ms |
63608 KB |
Output is correct |
6 |
Correct |
503 ms |
63736 KB |
Output is correct |
7 |
Correct |
522 ms |
63608 KB |
Output is correct |
8 |
Correct |
548 ms |
63840 KB |
Output is correct |
9 |
Correct |
461 ms |
63652 KB |
Output is correct |
10 |
Correct |
516 ms |
64044 KB |
Output is correct |
11 |
Correct |
424 ms |
63736 KB |
Output is correct |
12 |
Correct |
500 ms |
47624 KB |
Output is correct |
13 |
Correct |
602 ms |
63736 KB |
Output is correct |
14 |
Correct |
187 ms |
59444 KB |
Output is correct |
15 |
Correct |
168 ms |
59456 KB |
Output is correct |
16 |
Correct |
527 ms |
47612 KB |
Output is correct |
17 |
Correct |
610 ms |
46700 KB |
Output is correct |
18 |
Correct |
485 ms |
47364 KB |
Output is correct |
19 |
Correct |
235 ms |
47736 KB |
Output is correct |
20 |
Correct |
209 ms |
47736 KB |
Output is correct |
21 |
Correct |
208 ms |
47608 KB |
Output is correct |
22 |
Correct |
258 ms |
47752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
484 ms |
43144 KB |
Output is correct |
2 |
Correct |
205 ms |
47608 KB |
Output is correct |
3 |
Correct |
474 ms |
63544 KB |
Output is correct |
4 |
Correct |
518 ms |
63540 KB |
Output is correct |
5 |
Correct |
496 ms |
63608 KB |
Output is correct |
6 |
Correct |
503 ms |
63736 KB |
Output is correct |
7 |
Correct |
522 ms |
63608 KB |
Output is correct |
8 |
Correct |
548 ms |
63840 KB |
Output is correct |
9 |
Correct |
461 ms |
63652 KB |
Output is correct |
10 |
Correct |
516 ms |
64044 KB |
Output is correct |
11 |
Correct |
424 ms |
63736 KB |
Output is correct |
12 |
Correct |
500 ms |
47624 KB |
Output is correct |
13 |
Correct |
602 ms |
63736 KB |
Output is correct |
14 |
Correct |
187 ms |
59444 KB |
Output is correct |
15 |
Correct |
168 ms |
59456 KB |
Output is correct |
16 |
Correct |
527 ms |
47612 KB |
Output is correct |
17 |
Correct |
610 ms |
46700 KB |
Output is correct |
18 |
Correct |
485 ms |
47364 KB |
Output is correct |
19 |
Correct |
235 ms |
47736 KB |
Output is correct |
20 |
Correct |
209 ms |
47736 KB |
Output is correct |
21 |
Correct |
208 ms |
47608 KB |
Output is correct |
22 |
Correct |
258 ms |
47752 KB |
Output is correct |
23 |
Correct |
642 ms |
47828 KB |
Output is correct |
24 |
Correct |
339 ms |
50424 KB |
Output is correct |
25 |
Correct |
350 ms |
48888 KB |
Output is correct |
26 |
Correct |
411 ms |
46336 KB |
Output is correct |
27 |
Correct |
342 ms |
46328 KB |
Output is correct |
28 |
Correct |
425 ms |
49528 KB |
Output is correct |
29 |
Correct |
327 ms |
46328 KB |
Output is correct |
30 |
Correct |
391 ms |
47864 KB |
Output is correct |
31 |
Correct |
429 ms |
47736 KB |
Output is correct |
32 |
Correct |
414 ms |
46712 KB |
Output is correct |
33 |
Correct |
349 ms |
48376 KB |
Output is correct |
34 |
Correct |
496 ms |
47368 KB |
Output is correct |
35 |
Correct |
309 ms |
47232 KB |
Output is correct |
36 |
Correct |
196 ms |
61548 KB |
Output is correct |
37 |
Correct |
202 ms |
62268 KB |
Output is correct |
38 |
Correct |
521 ms |
47368 KB |
Output is correct |
39 |
Correct |
594 ms |
47188 KB |
Output is correct |
40 |
Correct |
677 ms |
47684 KB |
Output is correct |
41 |
Correct |
320 ms |
50280 KB |
Output is correct |
42 |
Correct |
297 ms |
50320 KB |
Output is correct |
43 |
Correct |
298 ms |
50220 KB |
Output is correct |
44 |
Correct |
265 ms |
50296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
14464 KB |
Output is correct |
2 |
Correct |
11 ms |
14592 KB |
Output is correct |
3 |
Correct |
10 ms |
14568 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
11 ms |
14464 KB |
Output is correct |
6 |
Correct |
11 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
10 ms |
14464 KB |
Output is correct |
9 |
Correct |
12 ms |
14712 KB |
Output is correct |
10 |
Correct |
12 ms |
14720 KB |
Output is correct |
11 |
Correct |
16 ms |
14720 KB |
Output is correct |
12 |
Correct |
16 ms |
14720 KB |
Output is correct |
13 |
Correct |
14 ms |
14848 KB |
Output is correct |
14 |
Correct |
14 ms |
14848 KB |
Output is correct |
15 |
Correct |
13 ms |
14848 KB |
Output is correct |
16 |
Correct |
12 ms |
14848 KB |
Output is correct |
17 |
Correct |
718 ms |
46484 KB |
Output is correct |
18 |
Correct |
724 ms |
53380 KB |
Output is correct |
19 |
Correct |
770 ms |
52932 KB |
Output is correct |
20 |
Correct |
748 ms |
52852 KB |
Output is correct |
21 |
Correct |
806 ms |
53052 KB |
Output is correct |
22 |
Correct |
790 ms |
54052 KB |
Output is correct |
23 |
Correct |
753 ms |
53696 KB |
Output is correct |
24 |
Correct |
801 ms |
54016 KB |
Output is correct |
25 |
Correct |
970 ms |
47252 KB |
Output is correct |
26 |
Correct |
883 ms |
53364 KB |
Output is correct |
27 |
Correct |
749 ms |
53156 KB |
Output is correct |
28 |
Correct |
742 ms |
53924 KB |
Output is correct |
29 |
Correct |
750 ms |
53132 KB |
Output is correct |
30 |
Correct |
738 ms |
53368 KB |
Output is correct |
31 |
Correct |
734 ms |
53276 KB |
Output is correct |
32 |
Correct |
769 ms |
53496 KB |
Output is correct |
33 |
Correct |
484 ms |
43144 KB |
Output is correct |
34 |
Correct |
205 ms |
47608 KB |
Output is correct |
35 |
Correct |
474 ms |
63544 KB |
Output is correct |
36 |
Correct |
518 ms |
63540 KB |
Output is correct |
37 |
Correct |
496 ms |
63608 KB |
Output is correct |
38 |
Correct |
503 ms |
63736 KB |
Output is correct |
39 |
Correct |
522 ms |
63608 KB |
Output is correct |
40 |
Correct |
548 ms |
63840 KB |
Output is correct |
41 |
Correct |
461 ms |
63652 KB |
Output is correct |
42 |
Correct |
516 ms |
64044 KB |
Output is correct |
43 |
Correct |
424 ms |
63736 KB |
Output is correct |
44 |
Correct |
500 ms |
47624 KB |
Output is correct |
45 |
Correct |
602 ms |
63736 KB |
Output is correct |
46 |
Correct |
187 ms |
59444 KB |
Output is correct |
47 |
Correct |
168 ms |
59456 KB |
Output is correct |
48 |
Correct |
527 ms |
47612 KB |
Output is correct |
49 |
Correct |
610 ms |
46700 KB |
Output is correct |
50 |
Correct |
485 ms |
47364 KB |
Output is correct |
51 |
Correct |
235 ms |
47736 KB |
Output is correct |
52 |
Correct |
209 ms |
47736 KB |
Output is correct |
53 |
Correct |
208 ms |
47608 KB |
Output is correct |
54 |
Correct |
258 ms |
47752 KB |
Output is correct |
55 |
Correct |
642 ms |
47828 KB |
Output is correct |
56 |
Correct |
339 ms |
50424 KB |
Output is correct |
57 |
Correct |
350 ms |
48888 KB |
Output is correct |
58 |
Correct |
411 ms |
46336 KB |
Output is correct |
59 |
Correct |
342 ms |
46328 KB |
Output is correct |
60 |
Correct |
425 ms |
49528 KB |
Output is correct |
61 |
Correct |
327 ms |
46328 KB |
Output is correct |
62 |
Correct |
391 ms |
47864 KB |
Output is correct |
63 |
Correct |
429 ms |
47736 KB |
Output is correct |
64 |
Correct |
414 ms |
46712 KB |
Output is correct |
65 |
Correct |
349 ms |
48376 KB |
Output is correct |
66 |
Correct |
496 ms |
47368 KB |
Output is correct |
67 |
Correct |
309 ms |
47232 KB |
Output is correct |
68 |
Correct |
196 ms |
61548 KB |
Output is correct |
69 |
Correct |
202 ms |
62268 KB |
Output is correct |
70 |
Correct |
521 ms |
47368 KB |
Output is correct |
71 |
Correct |
594 ms |
47188 KB |
Output is correct |
72 |
Correct |
677 ms |
47684 KB |
Output is correct |
73 |
Correct |
320 ms |
50280 KB |
Output is correct |
74 |
Correct |
297 ms |
50320 KB |
Output is correct |
75 |
Correct |
298 ms |
50220 KB |
Output is correct |
76 |
Correct |
265 ms |
50296 KB |
Output is correct |
77 |
Correct |
723 ms |
50500 KB |
Output is correct |
78 |
Correct |
680 ms |
52816 KB |
Output is correct |
79 |
Correct |
850 ms |
66048 KB |
Output is correct |
80 |
Correct |
810 ms |
65904 KB |
Output is correct |
81 |
Correct |
733 ms |
65952 KB |
Output is correct |
82 |
Correct |
723 ms |
65656 KB |
Output is correct |
83 |
Correct |
710 ms |
66272 KB |
Output is correct |
84 |
Correct |
882 ms |
66684 KB |
Output is correct |
85 |
Correct |
798 ms |
66040 KB |
Output is correct |
86 |
Correct |
834 ms |
66040 KB |
Output is correct |
87 |
Correct |
702 ms |
66168 KB |
Output is correct |
88 |
Correct |
813 ms |
49988 KB |
Output is correct |
89 |
Correct |
951 ms |
66296 KB |
Output is correct |
90 |
Correct |
667 ms |
70708 KB |
Output is correct |
91 |
Correct |
696 ms |
71476 KB |
Output is correct |
92 |
Correct |
862 ms |
50060 KB |
Output is correct |
93 |
Correct |
1014 ms |
49596 KB |
Output is correct |
94 |
Correct |
950 ms |
49188 KB |
Output is correct |
95 |
Correct |
675 ms |
52600 KB |
Output is correct |
96 |
Correct |
765 ms |
52728 KB |
Output is correct |
97 |
Correct |
665 ms |
52856 KB |
Output is correct |
98 |
Correct |
683 ms |
52600 KB |
Output is correct |