#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX 300005
int numNode, numEdge;
vector<int> G[MAX];
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){return (x ^ u ^ v);}
} edge[MAX];
int minDist[2][MAX];
void dijkstra(int s, int minDist[]){
for (int i = 1; i <= numNode; ++i) minDist[i] = LINF;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.emplace(0, s);
minDist[s] = 0;
while(q.size()){
int du, u; tie(du, u) = q.top(); q.pop();
if(du > minDist[u]) continue;
for(int&i : G[u]) {
int v = edge[i].other(u);
if(minimize(minDist[v], minDist[u] + edge[i].w)){
q.emplace(minDist[v], v);
}
}
}
}
int low[MAX], num[MAX], timeDfs = 0;
int suffix[MAX];
bool contain[MAX];
vector<pair<int, int>> newG[MAX];
bool ok = 0;
int value = 0;
void tarjan(int u, int p){
if(u == numNode) contain[u] = true;
low[u] = num[u] = ++timeDfs;
for (pair<int, int>&x : newG[u]){
int v = x.first, i = x.second;
if(i ^ p){
if(num[v]) minimize(low[u], num[v]);
else{
tarjan(v, i);
minimize(low[u], low[v]);
contain[u] |= contain[v];
if(num[v] == low[v]){
if(!contain[v]) continue;
int eu = edge[i].u, ev = edge[i].v;
if(minDist[0][eu] + edge[i].w + minDist[1][ev] != minDist[0][numNode]) continue;
if(minDist[0][eu] + edge[i].w + minDist[1][ev] + suffix[i] >= minDist[0][numNode] + value)
ok = 1;
}
}
}
}
}
void process(void){
cin >> numNode >> numEdge;
int max_weight = 0;
for (int i = 1; i <= numEdge; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
maximize(max_weight, edge[i].w);
}
dijkstra(1, minDist[0]);
dijkstra(numNode, minDist[1]);
suffix[numEdge] = -LINF;
for (int i = numEdge - 1; i >= 1; --i){
maximize(suffix[i], suffix[i + 1]);
maximize(suffix[i], edge[i + 1].w);
}
for (int i = 1; i <= numEdge; ++i){
if(minDist[0][edge[i].u] > minDist[0][edge[i].v]) swap(edge[i].u, edge[i].v);
}
int l = 0, r = max_weight + 1;
int add = 0;
auto check = [&](int val) -> bool{
timeDfs = 0, ok = 0;
value = val;
for (int i = 1; i <= numNode; ++i) newG[i].clear(), contain[i] = num[i] = low[i] = 0;
auto active = [&](int u, int v, int w, int x) -> bool{
if(minDist[0][u] + minDist[1][v] + w < minDist[0][numNode] + x) return true;
if(minDist[0][v] + minDist[1][u] + w < minDist[0][numNode] + x) return true;
return false;
};
for (int u = 1; u <= numNode; ++u){
for (int&i : G[u]){
int v = edge[i].other(u);
if(active(u, v, edge[i].w, val)) {
newG[u].emplace_back(v, i);
}
}
}
tarjan(1, 0);
return (ok);
};
while(l <= r){
int m = (l + r) >> 1;
if(check(m)) l = m + 1, add = m;
else r = m - 1;
}
cout << minDist[0][numNode] + add;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
Compilation message
Aesthetic.cpp: In lambda function:
Aesthetic.cpp:128:81: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
128 | for (int i = 1; i <= numNode; ++i) newG[i].clear(), contain[i] = num[i] = low[i] = 0;
| ~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14620 KB |
Output is correct |
3 |
Correct |
6 ms |
14568 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14620 KB |
Output is correct |
3 |
Correct |
6 ms |
14568 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14684 KB |
Output is correct |
10 |
Correct |
7 ms |
14832 KB |
Output is correct |
11 |
Correct |
8 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14828 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14684 KB |
Output is correct |
16 |
Correct |
7 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
639 ms |
51032 KB |
Output is correct |
2 |
Correct |
634 ms |
51256 KB |
Output is correct |
3 |
Correct |
578 ms |
50648 KB |
Output is correct |
4 |
Correct |
618 ms |
50772 KB |
Output is correct |
5 |
Correct |
630 ms |
51092 KB |
Output is correct |
6 |
Correct |
675 ms |
51796 KB |
Output is correct |
7 |
Correct |
638 ms |
51536 KB |
Output is correct |
8 |
Correct |
623 ms |
51812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
691 ms |
51796 KB |
Output is correct |
2 |
Correct |
646 ms |
51280 KB |
Output is correct |
3 |
Correct |
602 ms |
51024 KB |
Output is correct |
4 |
Correct |
595 ms |
51748 KB |
Output is correct |
5 |
Correct |
611 ms |
50900 KB |
Output is correct |
6 |
Correct |
620 ms |
51024 KB |
Output is correct |
7 |
Correct |
640 ms |
51032 KB |
Output is correct |
8 |
Correct |
603 ms |
51376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
44992 KB |
Output is correct |
2 |
Correct |
131 ms |
46416 KB |
Output is correct |
3 |
Correct |
186 ms |
59732 KB |
Output is correct |
4 |
Correct |
171 ms |
59736 KB |
Output is correct |
5 |
Correct |
183 ms |
59828 KB |
Output is correct |
6 |
Correct |
181 ms |
60096 KB |
Output is correct |
7 |
Correct |
177 ms |
59728 KB |
Output is correct |
8 |
Correct |
169 ms |
59940 KB |
Output is correct |
9 |
Correct |
175 ms |
59732 KB |
Output is correct |
10 |
Correct |
195 ms |
60240 KB |
Output is correct |
11 |
Correct |
184 ms |
59984 KB |
Output is correct |
12 |
Correct |
212 ms |
45156 KB |
Output is correct |
13 |
Correct |
171 ms |
59984 KB |
Output is correct |
14 |
Correct |
118 ms |
75332 KB |
Output is correct |
15 |
Correct |
113 ms |
75344 KB |
Output is correct |
16 |
Correct |
237 ms |
44932 KB |
Output is correct |
17 |
Correct |
215 ms |
44116 KB |
Output is correct |
18 |
Correct |
215 ms |
44720 KB |
Output is correct |
19 |
Correct |
141 ms |
46240 KB |
Output is correct |
20 |
Correct |
125 ms |
46416 KB |
Output is correct |
21 |
Correct |
126 ms |
46416 KB |
Output is correct |
22 |
Correct |
127 ms |
46440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
44992 KB |
Output is correct |
2 |
Correct |
131 ms |
46416 KB |
Output is correct |
3 |
Correct |
186 ms |
59732 KB |
Output is correct |
4 |
Correct |
171 ms |
59736 KB |
Output is correct |
5 |
Correct |
183 ms |
59828 KB |
Output is correct |
6 |
Correct |
181 ms |
60096 KB |
Output is correct |
7 |
Correct |
177 ms |
59728 KB |
Output is correct |
8 |
Correct |
169 ms |
59940 KB |
Output is correct |
9 |
Correct |
175 ms |
59732 KB |
Output is correct |
10 |
Correct |
195 ms |
60240 KB |
Output is correct |
11 |
Correct |
184 ms |
59984 KB |
Output is correct |
12 |
Correct |
212 ms |
45156 KB |
Output is correct |
13 |
Correct |
171 ms |
59984 KB |
Output is correct |
14 |
Correct |
118 ms |
75332 KB |
Output is correct |
15 |
Correct |
113 ms |
75344 KB |
Output is correct |
16 |
Correct |
237 ms |
44932 KB |
Output is correct |
17 |
Correct |
215 ms |
44116 KB |
Output is correct |
18 |
Correct |
215 ms |
44720 KB |
Output is correct |
19 |
Correct |
141 ms |
46240 KB |
Output is correct |
20 |
Correct |
125 ms |
46416 KB |
Output is correct |
21 |
Correct |
126 ms |
46416 KB |
Output is correct |
22 |
Correct |
127 ms |
46440 KB |
Output is correct |
23 |
Correct |
322 ms |
45176 KB |
Output is correct |
24 |
Correct |
183 ms |
48980 KB |
Output is correct |
25 |
Correct |
167 ms |
43344 KB |
Output is correct |
26 |
Correct |
151 ms |
41704 KB |
Output is correct |
27 |
Correct |
176 ms |
41664 KB |
Output is correct |
28 |
Incorrect |
145 ms |
43860 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14620 KB |
Output is correct |
3 |
Correct |
6 ms |
14568 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
9 |
Correct |
7 ms |
14684 KB |
Output is correct |
10 |
Correct |
7 ms |
14832 KB |
Output is correct |
11 |
Correct |
8 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14828 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
7 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14684 KB |
Output is correct |
16 |
Correct |
7 ms |
14712 KB |
Output is correct |
17 |
Correct |
639 ms |
51032 KB |
Output is correct |
18 |
Correct |
634 ms |
51256 KB |
Output is correct |
19 |
Correct |
578 ms |
50648 KB |
Output is correct |
20 |
Correct |
618 ms |
50772 KB |
Output is correct |
21 |
Correct |
630 ms |
51092 KB |
Output is correct |
22 |
Correct |
675 ms |
51796 KB |
Output is correct |
23 |
Correct |
638 ms |
51536 KB |
Output is correct |
24 |
Correct |
623 ms |
51812 KB |
Output is correct |
25 |
Correct |
691 ms |
51796 KB |
Output is correct |
26 |
Correct |
646 ms |
51280 KB |
Output is correct |
27 |
Correct |
602 ms |
51024 KB |
Output is correct |
28 |
Correct |
595 ms |
51748 KB |
Output is correct |
29 |
Correct |
611 ms |
50900 KB |
Output is correct |
30 |
Correct |
620 ms |
51024 KB |
Output is correct |
31 |
Correct |
640 ms |
51032 KB |
Output is correct |
32 |
Correct |
603 ms |
51376 KB |
Output is correct |
33 |
Correct |
229 ms |
44992 KB |
Output is correct |
34 |
Correct |
131 ms |
46416 KB |
Output is correct |
35 |
Correct |
186 ms |
59732 KB |
Output is correct |
36 |
Correct |
171 ms |
59736 KB |
Output is correct |
37 |
Correct |
183 ms |
59828 KB |
Output is correct |
38 |
Correct |
181 ms |
60096 KB |
Output is correct |
39 |
Correct |
177 ms |
59728 KB |
Output is correct |
40 |
Correct |
169 ms |
59940 KB |
Output is correct |
41 |
Correct |
175 ms |
59732 KB |
Output is correct |
42 |
Correct |
195 ms |
60240 KB |
Output is correct |
43 |
Correct |
184 ms |
59984 KB |
Output is correct |
44 |
Correct |
212 ms |
45156 KB |
Output is correct |
45 |
Correct |
171 ms |
59984 KB |
Output is correct |
46 |
Correct |
118 ms |
75332 KB |
Output is correct |
47 |
Correct |
113 ms |
75344 KB |
Output is correct |
48 |
Correct |
237 ms |
44932 KB |
Output is correct |
49 |
Correct |
215 ms |
44116 KB |
Output is correct |
50 |
Correct |
215 ms |
44720 KB |
Output is correct |
51 |
Correct |
141 ms |
46240 KB |
Output is correct |
52 |
Correct |
125 ms |
46416 KB |
Output is correct |
53 |
Correct |
126 ms |
46416 KB |
Output is correct |
54 |
Correct |
127 ms |
46440 KB |
Output is correct |
55 |
Correct |
322 ms |
45176 KB |
Output is correct |
56 |
Correct |
183 ms |
48980 KB |
Output is correct |
57 |
Correct |
167 ms |
43344 KB |
Output is correct |
58 |
Correct |
151 ms |
41704 KB |
Output is correct |
59 |
Correct |
176 ms |
41664 KB |
Output is correct |
60 |
Incorrect |
145 ms |
43860 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |