#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
const int logn = 19;
const long long inf = 1e16;
int anc[logn + 1][maxn];
int l[maxn], r[maxn], w[maxn];
vector <int> g[maxn], t[maxn];
long long ans[maxn];
int dep[maxn];
int n, m;
struct data {
long long dist;
int node;
data (long long dist, int node) : dist(dist), node(node) {}
data () {}
bool operator < (data d) const {
return dist > d.dist;
}
};
vector <long long> dijstra(int root) {
priority_queue <data> Q;
vector <long long> dist (n, inf);
Q.emplace(0, root);
dist[root] = 0;
while(!Q.empty()) {
int node = Q.top().node;
long long var = Q.top().dist;
Q.pop();
if(var != dist[node]) continue;
for(int e : g[node]) {
int x = l[e] ^ r[e] ^ node;
if(w[e] + dist[node] < dist[x]) {
dist[x] = w[e] + dist[node];
Q.emplace(dist[x], x);
}
}
}
return dist;
}
void dfs(int x) {
for(int i = 1; i <= logn; i++) {
anc[i][x] = anc[i - 1][anc[i - 1][x]];
}
for(int i : t[x]) {
dep[i] = 1 + dep[x];
anc[0][i] = x;
dfs(i);
}
}
int lca(int x, int y) {
if(dep[x] > dep[y]) swap(x, y);
for(int i = logn; i >= 0; i--) {
if(dep[y] - (1 << i) >= dep[x]) {
y = anc[i][y];
}
}
if(x == y) return x;
for(int i = logn; i >= 0; i--) {
if(anc[i][x] != anc[i][y]) {
x = anc[i][x];
y = anc[i][y];
}
}
return anc[0][x];
}
long long prop[maxn * 4];
void update(int x, int y, long long val, int c = 1, int b = 0, int e = n) {
if(x <= b && e <= y) {
prop[c] = min(prop[c], val);
return ;
}
if(y < b || e < x) return ;
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
update(x, y, val, l, b, m);
update(x, y, val, r, m + 1, e);
}
long long get(int x, int c = 1, int b = 0, int e = n) {
if(b == e) return prop[c];
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
if(x <= m) return min(prop[c], get(x, l, b, m));
else return min(prop[c], get(x, r, m + 1, e));
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d %d", &l[i], &r[i], &w[i]);
l[i] -= 1;
r[i] -= 1;
g[l[i]].push_back(i);
g[r[i]].push_back(i);
}
for(int i = 0; i <= 4 * (n + 1); i++) prop[i] = inf;
vector <long long> forward = dijstra(0);
vector <long long> backward = dijstra(n - 1);
vector <int> par (n);
for(int i = 1; i < n; i++) {
for(int e : g[i]) {
int x = l[e] ^ r[e] ^ i;
if(forward[x] + w[e] == forward[i]) {
par[i] = e;
t[x].push_back(i);
break;
}
}
}
dfs(0);
int cur = n - 1;
vector <bool> onPath (m);
vector <int> id (n);
while(cur != 0) {
int e = par[cur];
onPath[e] = true;
cur = anc[0][cur];
id[dep[cur]] = e;
}
for(int i = 0; i < m; i++) ans[i] = inf;
for(int i = 0; i < m; i++) {
if(onPath[i]) continue;
ans[i] = forward[n - 1];
int p = lca(l[i], n - 1);
int q = lca(r[i], n - 1);
if(dep[p] > dep[q]) {
swap(p, q);
}
long long cost = min(w[i] + forward[l[i]] + backward[r[i]],
w[i] + forward[r[i]] + backward[l[i]]);
if(dep[p] < dep[q]) {
update(dep[p], dep[q] - 1, cost);
}
}
for(int i = 0; i < dep[n - 1]; i++) {
ans[id[i]] = get(i);
}
int mx = w[m - 1];
long long res = 0;
for(int i = m - 2; i >= 0; i--) {
long long p = forward[l[i]] + mx + w[i] + backward[r[i]];
long long q = forward[r[i]] + mx + w[i] + backward[l[i]];
res = max(res, min({ans[i], p, q}));
mx = max(mx, w[i]);
}
printf("%lld\n", res);
return 0;
}
Compilation message
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
Aesthetic.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &l[i], &r[i], &w[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14592 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
12 ms |
14592 KB |
Output is correct |
5 |
Correct |
9 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
10 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14592 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
12 ms |
14592 KB |
Output is correct |
5 |
Correct |
9 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
10 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14972 KB |
Output is correct |
10 |
Correct |
11 ms |
14848 KB |
Output is correct |
11 |
Correct |
11 ms |
14848 KB |
Output is correct |
12 |
Correct |
11 ms |
14848 KB |
Output is correct |
13 |
Correct |
11 ms |
14720 KB |
Output is correct |
14 |
Correct |
11 ms |
14720 KB |
Output is correct |
15 |
Correct |
11 ms |
14720 KB |
Output is correct |
16 |
Correct |
11 ms |
14720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1234 ms |
79128 KB |
Output is correct |
2 |
Correct |
1202 ms |
79644 KB |
Output is correct |
3 |
Correct |
1286 ms |
78668 KB |
Output is correct |
4 |
Correct |
1297 ms |
78968 KB |
Output is correct |
5 |
Correct |
1387 ms |
78892 KB |
Output is correct |
6 |
Correct |
1493 ms |
80332 KB |
Output is correct |
7 |
Correct |
1405 ms |
79992 KB |
Output is correct |
8 |
Correct |
1253 ms |
80376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1346 ms |
80304 KB |
Output is correct |
2 |
Correct |
1520 ms |
79408 KB |
Output is correct |
3 |
Correct |
1407 ms |
79284 KB |
Output is correct |
4 |
Correct |
1175 ms |
80400 KB |
Output is correct |
5 |
Correct |
1240 ms |
79224 KB |
Output is correct |
6 |
Correct |
1181 ms |
79480 KB |
Output is correct |
7 |
Correct |
1484 ms |
79424 KB |
Output is correct |
8 |
Correct |
1450 ms |
79736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
914 ms |
62936 KB |
Output is correct |
2 |
Correct |
347 ms |
73976 KB |
Output is correct |
3 |
Correct |
438 ms |
45304 KB |
Output is correct |
4 |
Correct |
427 ms |
45048 KB |
Output is correct |
5 |
Correct |
430 ms |
45236 KB |
Output is correct |
6 |
Correct |
462 ms |
45176 KB |
Output is correct |
7 |
Correct |
509 ms |
45176 KB |
Output is correct |
8 |
Correct |
444 ms |
45304 KB |
Output is correct |
9 |
Correct |
419 ms |
45048 KB |
Output is correct |
10 |
Correct |
429 ms |
45304 KB |
Output is correct |
11 |
Correct |
457 ms |
45176 KB |
Output is correct |
12 |
Correct |
859 ms |
62200 KB |
Output is correct |
13 |
Correct |
419 ms |
45176 KB |
Output is correct |
14 |
Correct |
263 ms |
68472 KB |
Output is correct |
15 |
Correct |
247 ms |
65272 KB |
Output is correct |
16 |
Correct |
829 ms |
60992 KB |
Output is correct |
17 |
Correct |
824 ms |
59952 KB |
Output is correct |
18 |
Correct |
852 ms |
60928 KB |
Output is correct |
19 |
Correct |
343 ms |
74468 KB |
Output is correct |
20 |
Correct |
341 ms |
74488 KB |
Output is correct |
21 |
Correct |
366 ms |
74448 KB |
Output is correct |
22 |
Correct |
358 ms |
74336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
914 ms |
62936 KB |
Output is correct |
2 |
Correct |
347 ms |
73976 KB |
Output is correct |
3 |
Correct |
438 ms |
45304 KB |
Output is correct |
4 |
Correct |
427 ms |
45048 KB |
Output is correct |
5 |
Correct |
430 ms |
45236 KB |
Output is correct |
6 |
Correct |
462 ms |
45176 KB |
Output is correct |
7 |
Correct |
509 ms |
45176 KB |
Output is correct |
8 |
Correct |
444 ms |
45304 KB |
Output is correct |
9 |
Correct |
419 ms |
45048 KB |
Output is correct |
10 |
Correct |
429 ms |
45304 KB |
Output is correct |
11 |
Correct |
457 ms |
45176 KB |
Output is correct |
12 |
Correct |
859 ms |
62200 KB |
Output is correct |
13 |
Correct |
419 ms |
45176 KB |
Output is correct |
14 |
Correct |
263 ms |
68472 KB |
Output is correct |
15 |
Correct |
247 ms |
65272 KB |
Output is correct |
16 |
Correct |
829 ms |
60992 KB |
Output is correct |
17 |
Correct |
824 ms |
59952 KB |
Output is correct |
18 |
Correct |
852 ms |
60928 KB |
Output is correct |
19 |
Correct |
343 ms |
74468 KB |
Output is correct |
20 |
Correct |
341 ms |
74488 KB |
Output is correct |
21 |
Correct |
366 ms |
74448 KB |
Output is correct |
22 |
Correct |
358 ms |
74336 KB |
Output is correct |
23 |
Correct |
885 ms |
62276 KB |
Output is correct |
24 |
Correct |
317 ms |
52856 KB |
Output is correct |
25 |
Correct |
453 ms |
44152 KB |
Output is correct |
26 |
Correct |
461 ms |
44280 KB |
Output is correct |
27 |
Correct |
461 ms |
44280 KB |
Output is correct |
28 |
Correct |
447 ms |
40952 KB |
Output is correct |
29 |
Correct |
464 ms |
41080 KB |
Output is correct |
30 |
Correct |
448 ms |
40952 KB |
Output is correct |
31 |
Correct |
468 ms |
41208 KB |
Output is correct |
32 |
Correct |
455 ms |
41208 KB |
Output is correct |
33 |
Correct |
458 ms |
41084 KB |
Output is correct |
34 |
Correct |
768 ms |
57976 KB |
Output is correct |
35 |
Correct |
541 ms |
41244 KB |
Output is correct |
36 |
Correct |
237 ms |
58032 KB |
Output is correct |
37 |
Correct |
209 ms |
38720 KB |
Output is correct |
38 |
Incorrect |
790 ms |
58772 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14592 KB |
Output is correct |
2 |
Correct |
9 ms |
14592 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
12 ms |
14592 KB |
Output is correct |
5 |
Correct |
9 ms |
14592 KB |
Output is correct |
6 |
Correct |
9 ms |
14592 KB |
Output is correct |
7 |
Correct |
10 ms |
14592 KB |
Output is correct |
8 |
Correct |
9 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14972 KB |
Output is correct |
10 |
Correct |
11 ms |
14848 KB |
Output is correct |
11 |
Correct |
11 ms |
14848 KB |
Output is correct |
12 |
Correct |
11 ms |
14848 KB |
Output is correct |
13 |
Correct |
11 ms |
14720 KB |
Output is correct |
14 |
Correct |
11 ms |
14720 KB |
Output is correct |
15 |
Correct |
11 ms |
14720 KB |
Output is correct |
16 |
Correct |
11 ms |
14720 KB |
Output is correct |
17 |
Correct |
1234 ms |
79128 KB |
Output is correct |
18 |
Correct |
1202 ms |
79644 KB |
Output is correct |
19 |
Correct |
1286 ms |
78668 KB |
Output is correct |
20 |
Correct |
1297 ms |
78968 KB |
Output is correct |
21 |
Correct |
1387 ms |
78892 KB |
Output is correct |
22 |
Correct |
1493 ms |
80332 KB |
Output is correct |
23 |
Correct |
1405 ms |
79992 KB |
Output is correct |
24 |
Correct |
1253 ms |
80376 KB |
Output is correct |
25 |
Correct |
1346 ms |
80304 KB |
Output is correct |
26 |
Correct |
1520 ms |
79408 KB |
Output is correct |
27 |
Correct |
1407 ms |
79284 KB |
Output is correct |
28 |
Correct |
1175 ms |
80400 KB |
Output is correct |
29 |
Correct |
1240 ms |
79224 KB |
Output is correct |
30 |
Correct |
1181 ms |
79480 KB |
Output is correct |
31 |
Correct |
1484 ms |
79424 KB |
Output is correct |
32 |
Correct |
1450 ms |
79736 KB |
Output is correct |
33 |
Correct |
914 ms |
62936 KB |
Output is correct |
34 |
Correct |
347 ms |
73976 KB |
Output is correct |
35 |
Correct |
438 ms |
45304 KB |
Output is correct |
36 |
Correct |
427 ms |
45048 KB |
Output is correct |
37 |
Correct |
430 ms |
45236 KB |
Output is correct |
38 |
Correct |
462 ms |
45176 KB |
Output is correct |
39 |
Correct |
509 ms |
45176 KB |
Output is correct |
40 |
Correct |
444 ms |
45304 KB |
Output is correct |
41 |
Correct |
419 ms |
45048 KB |
Output is correct |
42 |
Correct |
429 ms |
45304 KB |
Output is correct |
43 |
Correct |
457 ms |
45176 KB |
Output is correct |
44 |
Correct |
859 ms |
62200 KB |
Output is correct |
45 |
Correct |
419 ms |
45176 KB |
Output is correct |
46 |
Correct |
263 ms |
68472 KB |
Output is correct |
47 |
Correct |
247 ms |
65272 KB |
Output is correct |
48 |
Correct |
829 ms |
60992 KB |
Output is correct |
49 |
Correct |
824 ms |
59952 KB |
Output is correct |
50 |
Correct |
852 ms |
60928 KB |
Output is correct |
51 |
Correct |
343 ms |
74468 KB |
Output is correct |
52 |
Correct |
341 ms |
74488 KB |
Output is correct |
53 |
Correct |
366 ms |
74448 KB |
Output is correct |
54 |
Correct |
358 ms |
74336 KB |
Output is correct |
55 |
Correct |
885 ms |
62276 KB |
Output is correct |
56 |
Correct |
317 ms |
52856 KB |
Output is correct |
57 |
Correct |
453 ms |
44152 KB |
Output is correct |
58 |
Correct |
461 ms |
44280 KB |
Output is correct |
59 |
Correct |
461 ms |
44280 KB |
Output is correct |
60 |
Correct |
447 ms |
40952 KB |
Output is correct |
61 |
Correct |
464 ms |
41080 KB |
Output is correct |
62 |
Correct |
448 ms |
40952 KB |
Output is correct |
63 |
Correct |
468 ms |
41208 KB |
Output is correct |
64 |
Correct |
455 ms |
41208 KB |
Output is correct |
65 |
Correct |
458 ms |
41084 KB |
Output is correct |
66 |
Correct |
768 ms |
57976 KB |
Output is correct |
67 |
Correct |
541 ms |
41244 KB |
Output is correct |
68 |
Correct |
237 ms |
58032 KB |
Output is correct |
69 |
Correct |
209 ms |
38720 KB |
Output is correct |
70 |
Incorrect |
790 ms |
58772 KB |
Output isn't correct |
71 |
Halted |
0 ms |
0 KB |
- |