#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 = 1, r = max_weight;
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 |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14472 KB |
Output is correct |
4 |
Correct |
6 ms |
14612 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 ms |
14428 KB |
Output is correct |
8 |
Correct |
6 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14472 KB |
Output is correct |
4 |
Correct |
6 ms |
14612 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 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 |
14684 KB |
Output is correct |
11 |
Correct |
9 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14684 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
8 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14824 KB |
Output is correct |
16 |
Correct |
7 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
756 ms |
51028 KB |
Output is correct |
2 |
Correct |
726 ms |
51188 KB |
Output is correct |
3 |
Correct |
777 ms |
50852 KB |
Output is correct |
4 |
Correct |
800 ms |
50776 KB |
Output is correct |
5 |
Correct |
771 ms |
51000 KB |
Output is correct |
6 |
Correct |
824 ms |
51816 KB |
Output is correct |
7 |
Correct |
777 ms |
51356 KB |
Output is correct |
8 |
Correct |
775 ms |
51816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
721 ms |
51748 KB |
Output is correct |
2 |
Correct |
671 ms |
51332 KB |
Output is correct |
3 |
Correct |
740 ms |
51024 KB |
Output is correct |
4 |
Correct |
808 ms |
51768 KB |
Output is correct |
5 |
Correct |
762 ms |
51116 KB |
Output is correct |
6 |
Correct |
837 ms |
51100 KB |
Output is correct |
7 |
Correct |
817 ms |
51028 KB |
Output is correct |
8 |
Correct |
720 ms |
51536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
44992 KB |
Output is correct |
2 |
Correct |
128 ms |
45728 KB |
Output is correct |
3 |
Correct |
207 ms |
59792 KB |
Output is correct |
4 |
Correct |
189 ms |
59768 KB |
Output is correct |
5 |
Correct |
193 ms |
59732 KB |
Output is correct |
6 |
Correct |
182 ms |
59728 KB |
Output is correct |
7 |
Correct |
180 ms |
59732 KB |
Output is correct |
8 |
Correct |
183 ms |
60012 KB |
Output is correct |
9 |
Correct |
202 ms |
59728 KB |
Output is correct |
10 |
Correct |
185 ms |
60244 KB |
Output is correct |
11 |
Correct |
185 ms |
60020 KB |
Output is correct |
12 |
Correct |
253 ms |
45036 KB |
Output is correct |
13 |
Correct |
185 ms |
59988 KB |
Output is correct |
14 |
Correct |
95 ms |
56660 KB |
Output is correct |
15 |
Correct |
89 ms |
56540 KB |
Output is correct |
16 |
Correct |
207 ms |
45080 KB |
Output is correct |
17 |
Correct |
232 ms |
44348 KB |
Output is correct |
18 |
Correct |
256 ms |
44676 KB |
Output is correct |
19 |
Correct |
125 ms |
45904 KB |
Output is correct |
20 |
Correct |
133 ms |
45908 KB |
Output is correct |
21 |
Correct |
123 ms |
45908 KB |
Output is correct |
22 |
Correct |
127 ms |
45908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
44992 KB |
Output is correct |
2 |
Correct |
128 ms |
45728 KB |
Output is correct |
3 |
Correct |
207 ms |
59792 KB |
Output is correct |
4 |
Correct |
189 ms |
59768 KB |
Output is correct |
5 |
Correct |
193 ms |
59732 KB |
Output is correct |
6 |
Correct |
182 ms |
59728 KB |
Output is correct |
7 |
Correct |
180 ms |
59732 KB |
Output is correct |
8 |
Correct |
183 ms |
60012 KB |
Output is correct |
9 |
Correct |
202 ms |
59728 KB |
Output is correct |
10 |
Correct |
185 ms |
60244 KB |
Output is correct |
11 |
Correct |
185 ms |
60020 KB |
Output is correct |
12 |
Correct |
253 ms |
45036 KB |
Output is correct |
13 |
Correct |
185 ms |
59988 KB |
Output is correct |
14 |
Correct |
95 ms |
56660 KB |
Output is correct |
15 |
Correct |
89 ms |
56540 KB |
Output is correct |
16 |
Correct |
207 ms |
45080 KB |
Output is correct |
17 |
Correct |
232 ms |
44348 KB |
Output is correct |
18 |
Correct |
256 ms |
44676 KB |
Output is correct |
19 |
Correct |
125 ms |
45904 KB |
Output is correct |
20 |
Correct |
133 ms |
45908 KB |
Output is correct |
21 |
Correct |
123 ms |
45908 KB |
Output is correct |
22 |
Correct |
127 ms |
45908 KB |
Output is correct |
23 |
Correct |
272 ms |
45160 KB |
Output is correct |
24 |
Correct |
181 ms |
48464 KB |
Output is correct |
25 |
Correct |
152 ms |
43532 KB |
Output is correct |
26 |
Correct |
149 ms |
41552 KB |
Output is correct |
27 |
Correct |
174 ms |
42088 KB |
Output is correct |
28 |
Correct |
148 ms |
43892 KB |
Output is correct |
29 |
Correct |
150 ms |
41812 KB |
Output is correct |
30 |
Correct |
153 ms |
42832 KB |
Output is correct |
31 |
Correct |
152 ms |
42576 KB |
Output is correct |
32 |
Correct |
148 ms |
42064 KB |
Output is correct |
33 |
Correct |
162 ms |
43092 KB |
Output is correct |
34 |
Correct |
292 ms |
44936 KB |
Output is correct |
35 |
Correct |
147 ms |
42580 KB |
Output is correct |
36 |
Correct |
110 ms |
59448 KB |
Output is correct |
37 |
Correct |
127 ms |
60164 KB |
Output is correct |
38 |
Correct |
293 ms |
44884 KB |
Output is correct |
39 |
Correct |
265 ms |
44504 KB |
Output is correct |
40 |
Correct |
279 ms |
45232 KB |
Output is correct |
41 |
Correct |
177 ms |
48464 KB |
Output is correct |
42 |
Correct |
183 ms |
48464 KB |
Output is correct |
43 |
Correct |
190 ms |
48468 KB |
Output is correct |
44 |
Correct |
179 ms |
48464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14424 KB |
Output is correct |
2 |
Correct |
7 ms |
14424 KB |
Output is correct |
3 |
Correct |
6 ms |
14472 KB |
Output is correct |
4 |
Correct |
6 ms |
14612 KB |
Output is correct |
5 |
Correct |
6 ms |
14424 KB |
Output is correct |
6 |
Correct |
7 ms |
14428 KB |
Output is correct |
7 |
Correct |
6 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 |
14684 KB |
Output is correct |
11 |
Correct |
9 ms |
14680 KB |
Output is correct |
12 |
Correct |
8 ms |
14684 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
8 ms |
14680 KB |
Output is correct |
15 |
Correct |
7 ms |
14824 KB |
Output is correct |
16 |
Correct |
7 ms |
14684 KB |
Output is correct |
17 |
Correct |
756 ms |
51028 KB |
Output is correct |
18 |
Correct |
726 ms |
51188 KB |
Output is correct |
19 |
Correct |
777 ms |
50852 KB |
Output is correct |
20 |
Correct |
800 ms |
50776 KB |
Output is correct |
21 |
Correct |
771 ms |
51000 KB |
Output is correct |
22 |
Correct |
824 ms |
51816 KB |
Output is correct |
23 |
Correct |
777 ms |
51356 KB |
Output is correct |
24 |
Correct |
775 ms |
51816 KB |
Output is correct |
25 |
Correct |
721 ms |
51748 KB |
Output is correct |
26 |
Correct |
671 ms |
51332 KB |
Output is correct |
27 |
Correct |
740 ms |
51024 KB |
Output is correct |
28 |
Correct |
808 ms |
51768 KB |
Output is correct |
29 |
Correct |
762 ms |
51116 KB |
Output is correct |
30 |
Correct |
837 ms |
51100 KB |
Output is correct |
31 |
Correct |
817 ms |
51028 KB |
Output is correct |
32 |
Correct |
720 ms |
51536 KB |
Output is correct |
33 |
Correct |
222 ms |
44992 KB |
Output is correct |
34 |
Correct |
128 ms |
45728 KB |
Output is correct |
35 |
Correct |
207 ms |
59792 KB |
Output is correct |
36 |
Correct |
189 ms |
59768 KB |
Output is correct |
37 |
Correct |
193 ms |
59732 KB |
Output is correct |
38 |
Correct |
182 ms |
59728 KB |
Output is correct |
39 |
Correct |
180 ms |
59732 KB |
Output is correct |
40 |
Correct |
183 ms |
60012 KB |
Output is correct |
41 |
Correct |
202 ms |
59728 KB |
Output is correct |
42 |
Correct |
185 ms |
60244 KB |
Output is correct |
43 |
Correct |
185 ms |
60020 KB |
Output is correct |
44 |
Correct |
253 ms |
45036 KB |
Output is correct |
45 |
Correct |
185 ms |
59988 KB |
Output is correct |
46 |
Correct |
95 ms |
56660 KB |
Output is correct |
47 |
Correct |
89 ms |
56540 KB |
Output is correct |
48 |
Correct |
207 ms |
45080 KB |
Output is correct |
49 |
Correct |
232 ms |
44348 KB |
Output is correct |
50 |
Correct |
256 ms |
44676 KB |
Output is correct |
51 |
Correct |
125 ms |
45904 KB |
Output is correct |
52 |
Correct |
133 ms |
45908 KB |
Output is correct |
53 |
Correct |
123 ms |
45908 KB |
Output is correct |
54 |
Correct |
127 ms |
45908 KB |
Output is correct |
55 |
Correct |
272 ms |
45160 KB |
Output is correct |
56 |
Correct |
181 ms |
48464 KB |
Output is correct |
57 |
Correct |
152 ms |
43532 KB |
Output is correct |
58 |
Correct |
149 ms |
41552 KB |
Output is correct |
59 |
Correct |
174 ms |
42088 KB |
Output is correct |
60 |
Correct |
148 ms |
43892 KB |
Output is correct |
61 |
Correct |
150 ms |
41812 KB |
Output is correct |
62 |
Correct |
153 ms |
42832 KB |
Output is correct |
63 |
Correct |
152 ms |
42576 KB |
Output is correct |
64 |
Correct |
148 ms |
42064 KB |
Output is correct |
65 |
Correct |
162 ms |
43092 KB |
Output is correct |
66 |
Correct |
292 ms |
44936 KB |
Output is correct |
67 |
Correct |
147 ms |
42580 KB |
Output is correct |
68 |
Correct |
110 ms |
59448 KB |
Output is correct |
69 |
Correct |
127 ms |
60164 KB |
Output is correct |
70 |
Correct |
293 ms |
44884 KB |
Output is correct |
71 |
Correct |
265 ms |
44504 KB |
Output is correct |
72 |
Correct |
279 ms |
45232 KB |
Output is correct |
73 |
Correct |
177 ms |
48464 KB |
Output is correct |
74 |
Correct |
183 ms |
48464 KB |
Output is correct |
75 |
Correct |
190 ms |
48468 KB |
Output is correct |
76 |
Correct |
179 ms |
48464 KB |
Output is correct |
77 |
Correct |
604 ms |
47908 KB |
Output is correct |
78 |
Correct |
440 ms |
51280 KB |
Output is correct |
79 |
Correct |
414 ms |
62356 KB |
Output is correct |
80 |
Correct |
402 ms |
62216 KB |
Output is correct |
81 |
Correct |
402 ms |
62028 KB |
Output is correct |
82 |
Correct |
399 ms |
61780 KB |
Output is correct |
83 |
Correct |
406 ms |
62360 KB |
Output is correct |
84 |
Correct |
427 ms |
62544 KB |
Output is correct |
85 |
Correct |
424 ms |
62032 KB |
Output is correct |
86 |
Correct |
459 ms |
62280 KB |
Output is correct |
87 |
Correct |
448 ms |
62356 KB |
Output is correct |
88 |
Correct |
561 ms |
47416 KB |
Output is correct |
89 |
Correct |
394 ms |
62376 KB |
Output is correct |
90 |
Correct |
214 ms |
66892 KB |
Output is correct |
91 |
Correct |
221 ms |
71968 KB |
Output is correct |
92 |
Correct |
595 ms |
47640 KB |
Output is correct |
93 |
Correct |
588 ms |
47280 KB |
Output is correct |
94 |
Correct |
585 ms |
46604 KB |
Output is correct |
95 |
Correct |
446 ms |
50840 KB |
Output is correct |
96 |
Correct |
458 ms |
51024 KB |
Output is correct |
97 |
Correct |
476 ms |
50852 KB |
Output is correct |
98 |
Correct |
447 ms |
50900 KB |
Output is correct |