#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN=300'000;
constexpr int MAXM=300'000;
vector<pair<int,int>>adj[MAXN]; // (target, edgeindex)
vector<pair<int,int>> backadj[MAXN]; // target, edgeindex
int weight[MAXM];
int delta[MAXM];
int direction[MAXM]; // set to the index of the to-direction
long long dist[MAXN];
int liveliness[MAXN];
bool visited[MAXN];
int waiting[MAXN];
vector<int> bridges;
bool isbridge[MAXM];
//long long bridgescore[MAXM];
vector<pair<pair<int,int>, long long>> ranges; // <[begin, end), score>
struct rangepqcmp{
bool operator()(const pair<long long,int>& a, const pair<long long,int>& b) { return a.first > b.first; }
};
priority_queue<pair<long long,int>, vector<pair<long long,int>>, rangepqcmp> rangepq; // <score, end>
int tracks;
int arc[MAXN];
void dfs1(int index){
visited[index]=true;
for(const pair<int,int>& x : backadj[index]){
liveliness[x.first]++;
if(!visited[x.first])dfs1(x.first);
}
}
void dfs2(int index){
if(waiting[index]+1!=liveliness[index]){
waiting[index]++;
return;
}
tracks-=waiting[index];
if(backadj[index].empty())return; // reached the source
if(tracks==1&&backadj[index].size()==1){
const int bridge=backadj[index][0].second;
bridges.push_back(bridge);
isbridge[bridge]=true;
}
tracks+=static_cast<int>(backadj[index].size())-1;
for(const pair<int,int>& x : backadj[index]){
dfs2(x.first);
}
}
int main(){
ios_base::sync_with_stdio(false);
int n,m;
cin>>n>>m;
for(int i=0;i<m;++i){
int a,b,w;
cin>>a>>b>>w;
weight[i]=w;
--a;--b;
adj[a].emplace_back(b,i);
adj[b].emplace_back(a,i);
}
{
int best=numeric_limits<int>::min();
for(int i=m-1;i>=0;--i){
delta[i]=best;
best=max(best, weight[i]);
}
}
fill(dist,dist+n,numeric_limits<long long>::max());
fill(direction,direction+m,-1);
{
struct elem{
int source;
int target;
long long distance;
int edgeindex;
};
struct cmp{
bool operator()(const elem& a, const elem& b) const noexcept{
return a.distance>b.distance;
}
};
priority_queue<elem,vector<elem>,cmp> pq;
pq.push({-1,0,0,-1});
while(!pq.empty()){
elem tmp=pq.top();
pq.pop();
if(dist[tmp.target]!=numeric_limits<long long>::max()){
if(dist[tmp.target]==tmp.distance){
backadj[tmp.target].emplace_back(tmp.source,tmp.edgeindex);
}
continue;
}
dist[tmp.target]=tmp.distance;
backadj[tmp.target].emplace_back(tmp.source,tmp.edgeindex);
for(const pair<int,int>& pr: adj[tmp.target]){
if(direction[pr.second]==-1){
direction[pr.second]=pr.first;
pq.push({tmp.target,pr.first,tmp.distance+weight[pr.second],pr.second});
}
}
}
}
backadj[0].clear();
/*for(int i=0;i<n;++i){
cout<<i<<": ";
for(const auto& pr:backadj[i]){
cout<<pr.first<<' '<<pr.second<<" ";
}
cout<<endl;
}*/
dfs1(n-1);
/*cout<<"Liveliness:";
for(int i=0;i<n;++i){
cout<<' '<<liveliness[i];
}
cout<<endl;*/
fill(visited,visited+n,false);
tracks=1;
liveliness[n-1]=1;
dfs2(n-1);
/*cout<<"Bridges:";
for(auto x:bridges){
cout<<' '<<x;
}
cout<<endl;*/
if(bridges.empty()){
cout<<dist[n-1]<<'\n';
return 0;
}
/*for(int x : bridges){
bridgescore[x]=dist[n-1]+delta[x];
}*/
fill(arc,arc+n,numeric_limits<int>::max());
{
struct elem{
int target;
long long distance;
int arc;
};
struct cmp{
bool operator()(const elem& a, const elem& b) const noexcept{
return a.distance>b.distance;
}
};
priority_queue<elem,vector<elem>,cmp> pq;
pq.push({n-1,0,0});
while(!pq.empty()){
elem tmp=pq.top();
pq.pop();
if(arc[tmp.target]!=numeric_limits<int>::max()){
if(arc[tmp.target]>tmp.arc){
//cout<<tmp.target<<' '<<tmp.arc<<' '<<tmp.distance<<' '<<dist[tmp.target]<<endl;
ranges.push_back({{tmp.arc, arc[tmp.target]}, tmp.distance+dist[tmp.target]});
/*for(int i=tmp.arc;i!=arc[tmp.target];++i){
bridgescore[bridges[i]]=min(bridgescore[bridges[i]],tmp.distance+dist[tmp.target]);
}*/
}
continue;
}
arc[tmp.target]=tmp.arc;
for(const pair<int,int>& pr:adj[tmp.target]){
if(isbridge[pr.second]){
pq.push({pr.first,tmp.distance+weight[pr.second],tmp.arc+1});
}else {
pq.push({pr.first,tmp.distance+weight[pr.second],tmp.arc});
}
}
}
}
sort(ranges.begin(), ranges.end(), [](const pair<pair<int,int>, long long>& a, const pair<pair<int,int>, long long>& b) {
return a.first.first < b.first.first;
});
long long ans=dist[n-1];
auto range_it=ranges.begin();
for(int i=0;i<static_cast<int>(bridges.size());++i){
long long bmin=dist[n-1]+delta[bridges[i]];
while(!rangepq.empty()&&rangepq.top().second<=i)rangepq.pop();
while(range_it!=ranges.end()&&range_it->first.first==i){
rangepq.push({range_it->second,range_it->first.second});
++range_it;
}
if(!rangepq.empty())bmin=min(bmin,rangepq.top().first);
ans=max(ans,bmin);
}
/*for(const int x:bridges){
ans=max(ans,bridgescore[x]);
}*/
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
9 ms |
14464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
9 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14720 KB |
Output is correct |
10 |
Correct |
12 ms |
14720 KB |
Output is correct |
11 |
Correct |
11 ms |
14720 KB |
Output is correct |
12 |
Correct |
12 ms |
14720 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
11 ms |
14592 KB |
Output is correct |
16 |
Correct |
11 ms |
14720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
620 ms |
42876 KB |
Output is correct |
2 |
Correct |
624 ms |
42784 KB |
Output is correct |
3 |
Correct |
588 ms |
42608 KB |
Output is correct |
4 |
Correct |
580 ms |
42744 KB |
Output is correct |
5 |
Correct |
588 ms |
42908 KB |
Output is correct |
6 |
Correct |
667 ms |
43512 KB |
Output is correct |
7 |
Correct |
642 ms |
43128 KB |
Output is correct |
8 |
Correct |
617 ms |
43384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
593 ms |
43384 KB |
Output is correct |
2 |
Correct |
593 ms |
43128 KB |
Output is correct |
3 |
Correct |
596 ms |
42488 KB |
Output is correct |
4 |
Correct |
602 ms |
42488 KB |
Output is correct |
5 |
Correct |
595 ms |
42744 KB |
Output is correct |
6 |
Correct |
586 ms |
42744 KB |
Output is correct |
7 |
Correct |
597 ms |
42924 KB |
Output is correct |
8 |
Correct |
625 ms |
43208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
46248 KB |
Output is correct |
2 |
Correct |
258 ms |
40844 KB |
Output is correct |
3 |
Correct |
266 ms |
32120 KB |
Output is correct |
4 |
Correct |
279 ms |
32188 KB |
Output is correct |
5 |
Correct |
270 ms |
32252 KB |
Output is correct |
6 |
Correct |
298 ms |
32248 KB |
Output is correct |
7 |
Correct |
316 ms |
32212 KB |
Output is correct |
8 |
Correct |
296 ms |
32156 KB |
Output is correct |
9 |
Correct |
266 ms |
32248 KB |
Output is correct |
10 |
Correct |
267 ms |
32376 KB |
Output is correct |
11 |
Correct |
278 ms |
32248 KB |
Output is correct |
12 |
Correct |
518 ms |
46592 KB |
Output is correct |
13 |
Correct |
287 ms |
32248 KB |
Output is correct |
14 |
Correct |
213 ms |
44588 KB |
Output is correct |
15 |
Correct |
216 ms |
46952 KB |
Output is correct |
16 |
Correct |
514 ms |
46728 KB |
Output is correct |
17 |
Correct |
504 ms |
44900 KB |
Output is correct |
18 |
Correct |
530 ms |
46352 KB |
Output is correct |
19 |
Correct |
270 ms |
40952 KB |
Output is correct |
20 |
Correct |
273 ms |
40952 KB |
Output is correct |
21 |
Correct |
259 ms |
41016 KB |
Output is correct |
22 |
Correct |
268 ms |
40988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
528 ms |
46248 KB |
Output is correct |
2 |
Correct |
258 ms |
40844 KB |
Output is correct |
3 |
Correct |
266 ms |
32120 KB |
Output is correct |
4 |
Correct |
279 ms |
32188 KB |
Output is correct |
5 |
Correct |
270 ms |
32252 KB |
Output is correct |
6 |
Correct |
298 ms |
32248 KB |
Output is correct |
7 |
Correct |
316 ms |
32212 KB |
Output is correct |
8 |
Correct |
296 ms |
32156 KB |
Output is correct |
9 |
Correct |
266 ms |
32248 KB |
Output is correct |
10 |
Correct |
267 ms |
32376 KB |
Output is correct |
11 |
Correct |
278 ms |
32248 KB |
Output is correct |
12 |
Correct |
518 ms |
46592 KB |
Output is correct |
13 |
Correct |
287 ms |
32248 KB |
Output is correct |
14 |
Correct |
213 ms |
44588 KB |
Output is correct |
15 |
Correct |
216 ms |
46952 KB |
Output is correct |
16 |
Correct |
514 ms |
46728 KB |
Output is correct |
17 |
Correct |
504 ms |
44900 KB |
Output is correct |
18 |
Correct |
530 ms |
46352 KB |
Output is correct |
19 |
Correct |
270 ms |
40952 KB |
Output is correct |
20 |
Correct |
273 ms |
40952 KB |
Output is correct |
21 |
Correct |
259 ms |
41016 KB |
Output is correct |
22 |
Correct |
268 ms |
40988 KB |
Output is correct |
23 |
Correct |
641 ms |
46596 KB |
Output is correct |
24 |
Correct |
309 ms |
41236 KB |
Output is correct |
25 |
Correct |
273 ms |
30456 KB |
Output is correct |
26 |
Correct |
375 ms |
36036 KB |
Output is correct |
27 |
Correct |
367 ms |
35272 KB |
Output is correct |
28 |
Correct |
359 ms |
32204 KB |
Output is correct |
29 |
Correct |
387 ms |
36292 KB |
Output is correct |
30 |
Correct |
380 ms |
35656 KB |
Output is correct |
31 |
Correct |
385 ms |
35592 KB |
Output is correct |
32 |
Correct |
361 ms |
35404 KB |
Output is correct |
33 |
Correct |
374 ms |
32868 KB |
Output is correct |
34 |
Correct |
619 ms |
46364 KB |
Output is correct |
35 |
Correct |
348 ms |
33736 KB |
Output is correct |
36 |
Correct |
248 ms |
60820 KB |
Output is correct |
37 |
Correct |
271 ms |
60952 KB |
Output is correct |
38 |
Correct |
587 ms |
46268 KB |
Output is correct |
39 |
Correct |
557 ms |
46044 KB |
Output is correct |
40 |
Correct |
581 ms |
46572 KB |
Output is correct |
41 |
Correct |
298 ms |
41236 KB |
Output is correct |
42 |
Correct |
296 ms |
41196 KB |
Output is correct |
43 |
Correct |
299 ms |
41244 KB |
Output is correct |
44 |
Correct |
298 ms |
41332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14464 KB |
Output is correct |
2 |
Correct |
9 ms |
14464 KB |
Output is correct |
3 |
Correct |
11 ms |
14464 KB |
Output is correct |
4 |
Correct |
11 ms |
14464 KB |
Output is correct |
5 |
Correct |
10 ms |
14464 KB |
Output is correct |
6 |
Correct |
10 ms |
14464 KB |
Output is correct |
7 |
Correct |
11 ms |
14464 KB |
Output is correct |
8 |
Correct |
9 ms |
14464 KB |
Output is correct |
9 |
Correct |
10 ms |
14720 KB |
Output is correct |
10 |
Correct |
12 ms |
14720 KB |
Output is correct |
11 |
Correct |
11 ms |
14720 KB |
Output is correct |
12 |
Correct |
12 ms |
14720 KB |
Output is correct |
13 |
Correct |
12 ms |
14592 KB |
Output is correct |
14 |
Correct |
12 ms |
14592 KB |
Output is correct |
15 |
Correct |
11 ms |
14592 KB |
Output is correct |
16 |
Correct |
11 ms |
14720 KB |
Output is correct |
17 |
Correct |
620 ms |
42876 KB |
Output is correct |
18 |
Correct |
624 ms |
42784 KB |
Output is correct |
19 |
Correct |
588 ms |
42608 KB |
Output is correct |
20 |
Correct |
580 ms |
42744 KB |
Output is correct |
21 |
Correct |
588 ms |
42908 KB |
Output is correct |
22 |
Correct |
667 ms |
43512 KB |
Output is correct |
23 |
Correct |
642 ms |
43128 KB |
Output is correct |
24 |
Correct |
617 ms |
43384 KB |
Output is correct |
25 |
Correct |
593 ms |
43384 KB |
Output is correct |
26 |
Correct |
593 ms |
43128 KB |
Output is correct |
27 |
Correct |
596 ms |
42488 KB |
Output is correct |
28 |
Correct |
602 ms |
42488 KB |
Output is correct |
29 |
Correct |
595 ms |
42744 KB |
Output is correct |
30 |
Correct |
586 ms |
42744 KB |
Output is correct |
31 |
Correct |
597 ms |
42924 KB |
Output is correct |
32 |
Correct |
625 ms |
43208 KB |
Output is correct |
33 |
Correct |
528 ms |
46248 KB |
Output is correct |
34 |
Correct |
258 ms |
40844 KB |
Output is correct |
35 |
Correct |
266 ms |
32120 KB |
Output is correct |
36 |
Correct |
279 ms |
32188 KB |
Output is correct |
37 |
Correct |
270 ms |
32252 KB |
Output is correct |
38 |
Correct |
298 ms |
32248 KB |
Output is correct |
39 |
Correct |
316 ms |
32212 KB |
Output is correct |
40 |
Correct |
296 ms |
32156 KB |
Output is correct |
41 |
Correct |
266 ms |
32248 KB |
Output is correct |
42 |
Correct |
267 ms |
32376 KB |
Output is correct |
43 |
Correct |
278 ms |
32248 KB |
Output is correct |
44 |
Correct |
518 ms |
46592 KB |
Output is correct |
45 |
Correct |
287 ms |
32248 KB |
Output is correct |
46 |
Correct |
213 ms |
44588 KB |
Output is correct |
47 |
Correct |
216 ms |
46952 KB |
Output is correct |
48 |
Correct |
514 ms |
46728 KB |
Output is correct |
49 |
Correct |
504 ms |
44900 KB |
Output is correct |
50 |
Correct |
530 ms |
46352 KB |
Output is correct |
51 |
Correct |
270 ms |
40952 KB |
Output is correct |
52 |
Correct |
273 ms |
40952 KB |
Output is correct |
53 |
Correct |
259 ms |
41016 KB |
Output is correct |
54 |
Correct |
268 ms |
40988 KB |
Output is correct |
55 |
Correct |
641 ms |
46596 KB |
Output is correct |
56 |
Correct |
309 ms |
41236 KB |
Output is correct |
57 |
Correct |
273 ms |
30456 KB |
Output is correct |
58 |
Correct |
375 ms |
36036 KB |
Output is correct |
59 |
Correct |
367 ms |
35272 KB |
Output is correct |
60 |
Correct |
359 ms |
32204 KB |
Output is correct |
61 |
Correct |
387 ms |
36292 KB |
Output is correct |
62 |
Correct |
380 ms |
35656 KB |
Output is correct |
63 |
Correct |
385 ms |
35592 KB |
Output is correct |
64 |
Correct |
361 ms |
35404 KB |
Output is correct |
65 |
Correct |
374 ms |
32868 KB |
Output is correct |
66 |
Correct |
619 ms |
46364 KB |
Output is correct |
67 |
Correct |
348 ms |
33736 KB |
Output is correct |
68 |
Correct |
248 ms |
60820 KB |
Output is correct |
69 |
Correct |
271 ms |
60952 KB |
Output is correct |
70 |
Correct |
587 ms |
46268 KB |
Output is correct |
71 |
Correct |
557 ms |
46044 KB |
Output is correct |
72 |
Correct |
581 ms |
46572 KB |
Output is correct |
73 |
Correct |
298 ms |
41236 KB |
Output is correct |
74 |
Correct |
296 ms |
41196 KB |
Output is correct |
75 |
Correct |
299 ms |
41244 KB |
Output is correct |
76 |
Correct |
298 ms |
41332 KB |
Output is correct |
77 |
Correct |
668 ms |
46768 KB |
Output is correct |
78 |
Correct |
332 ms |
40976 KB |
Output is correct |
79 |
Correct |
392 ms |
36236 KB |
Output is correct |
80 |
Correct |
381 ms |
35912 KB |
Output is correct |
81 |
Correct |
386 ms |
36296 KB |
Output is correct |
82 |
Correct |
405 ms |
39360 KB |
Output is correct |
83 |
Correct |
406 ms |
39624 KB |
Output is correct |
84 |
Correct |
395 ms |
36460 KB |
Output is correct |
85 |
Correct |
414 ms |
36084 KB |
Output is correct |
86 |
Correct |
383 ms |
35912 KB |
Output is correct |
87 |
Correct |
423 ms |
36164 KB |
Output is correct |
88 |
Correct |
725 ms |
46352 KB |
Output is correct |
89 |
Correct |
438 ms |
36288 KB |
Output is correct |
90 |
Correct |
305 ms |
60948 KB |
Output is correct |
91 |
Correct |
297 ms |
53520 KB |
Output is correct |
92 |
Correct |
692 ms |
46712 KB |
Output is correct |
93 |
Correct |
732 ms |
45444 KB |
Output is correct |
94 |
Correct |
672 ms |
46196 KB |
Output is correct |
95 |
Correct |
338 ms |
41200 KB |
Output is correct |
96 |
Correct |
345 ms |
41104 KB |
Output is correct |
97 |
Correct |
330 ms |
40980 KB |
Output is correct |
98 |
Correct |
335 ms |
41072 KB |
Output is correct |