#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 300000
#define N_ (1 << 19) /* N_ = pow2(ceil(log2(N))) */
#define M 300000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
long long max(long long a, long long b) { return a > b ? a : b; }
int ii[M], jj[M], ww[M]; char path[M]; int m;
int *eh[N], eo[N]; long long dds[N], ddt[N]; int ff[N], gg[N], n;
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
long long *dd; int iq[N + 1], pq[N], cnt;
int lt(int i, int j) { return dd[i] < dd[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
void dijkstra(int n, int s) {
int i, o;
memset(dd, 0x3f, n * sizeof *dd);
memset(pq, 0, n * sizeof *pq), cnt = 0;
dd[s] = 0, ff[s] = -1, pq_add_last(s);
while (cnt) {
i = pq_remove_first();
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
long long d = dd[i] + ww[h];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, ff[j] = h, pq_up(j);
}
}
}
}
void dfs(int i, int g) {
int o;
gg[i] = g;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h];
long long d = dds[i] + ww[h];
if (dds[j] == d && gg[j] == -1)
dfs(j, g);
}
}
long long st[N_ * 2]; int n_;
void update(int l, int r, long long x) {
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
st[l] = min(st[l], x), l++;
if ((r & 1) == 0)
st[r] = min(st[r], x), r--;
}
}
int main() {
static int qu[N];
int g, h, i, j, l, r, tmp, w;
long long ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
ii[h] = i, jj[h] = j;
append(i, h), append(j, h);
}
dd = dds, dijkstra(n, 0);
dd = ddt, dijkstra(n, n - 1);
i = 0, cnt = 0;
while (i != n - 1) {
qu[cnt++] = i;
h = ff[i];
path[h] = 1, i ^= ii[h] ^ jj[h];
}
qu[cnt++] = i;
memset(gg, -1, n * sizeof *gg);
for (g = 0; g < cnt; g++)
gg[qu[g]] = g;
for (g = 0; g < cnt; g++)
dfs(qu[g], g);
n_ = 1;
while (n_ < cnt)
n_ <<= 1;
memset(st, 0x3f, n_ * 2 * sizeof *st);
for (h = 0; h < m; h++)
if (!path[h]) {
i = ii[h], j = jj[h], l = gg[i], r = gg[j];
if (l > r)
tmp = i, i = j, j = tmp, tmp = l, l = r, r = tmp;
if (l < r)
update(l, r - 1, dds[i] + ww[h] + ddt[j]);
}
for (i = 1; i < n_; i++) {
st[i << 1 | 0] = min(st[i << 1 | 0], st[i]);
st[i << 1 | 1] = min(st[i << 1 | 1], st[i]);
}
w = 0, ans = dds[n - 1];
for (h = m - 1; h >= 0; h--) {
if (path[h]) {
g = min(gg[ii[h]], gg[jj[h]]);
ans = max(ans, min(dds[n - 1] + w, st[n_ + g]));
}
w = max(w, ww[h]);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
Aesthetic.c: In function 'append':
Aesthetic.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Aesthetic.c: In function 'main':
Aesthetic.c:111:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Aesthetic.c:115:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
335 ms |
24896 KB |
Output is correct |
2 |
Correct |
348 ms |
25144 KB |
Output is correct |
3 |
Correct |
330 ms |
24780 KB |
Output is correct |
4 |
Correct |
333 ms |
24652 KB |
Output is correct |
5 |
Correct |
335 ms |
24844 KB |
Output is correct |
6 |
Correct |
344 ms |
25468 KB |
Output is correct |
7 |
Correct |
341 ms |
25312 KB |
Output is correct |
8 |
Correct |
383 ms |
25456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
370 ms |
25336 KB |
Output is correct |
2 |
Correct |
351 ms |
25004 KB |
Output is correct |
3 |
Correct |
358 ms |
25008 KB |
Output is correct |
4 |
Correct |
339 ms |
25332 KB |
Output is correct |
5 |
Correct |
323 ms |
24904 KB |
Output is correct |
6 |
Correct |
320 ms |
25072 KB |
Output is correct |
7 |
Correct |
337 ms |
24904 KB |
Output is correct |
8 |
Correct |
328 ms |
25036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
19444 KB |
Output is correct |
2 |
Correct |
117 ms |
22700 KB |
Output is correct |
3 |
Correct |
111 ms |
13404 KB |
Output is correct |
4 |
Correct |
108 ms |
13444 KB |
Output is correct |
5 |
Correct |
108 ms |
13516 KB |
Output is correct |
6 |
Correct |
113 ms |
13396 KB |
Output is correct |
7 |
Correct |
110 ms |
13516 KB |
Output is correct |
8 |
Correct |
110 ms |
13520 KB |
Output is correct |
9 |
Correct |
110 ms |
13512 KB |
Output is correct |
10 |
Correct |
116 ms |
13588 KB |
Output is correct |
11 |
Correct |
124 ms |
13480 KB |
Output is correct |
12 |
Correct |
228 ms |
19436 KB |
Output is correct |
13 |
Correct |
108 ms |
13516 KB |
Output is correct |
14 |
Correct |
91 ms |
28400 KB |
Output is correct |
15 |
Correct |
91 ms |
28428 KB |
Output is correct |
16 |
Correct |
218 ms |
19152 KB |
Output is correct |
17 |
Correct |
225 ms |
18640 KB |
Output is correct |
18 |
Correct |
206 ms |
19024 KB |
Output is correct |
19 |
Correct |
114 ms |
22860 KB |
Output is correct |
20 |
Correct |
116 ms |
22860 KB |
Output is correct |
21 |
Correct |
122 ms |
22860 KB |
Output is correct |
22 |
Correct |
115 ms |
22864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
19444 KB |
Output is correct |
2 |
Correct |
117 ms |
22700 KB |
Output is correct |
3 |
Correct |
111 ms |
13404 KB |
Output is correct |
4 |
Correct |
108 ms |
13444 KB |
Output is correct |
5 |
Correct |
108 ms |
13516 KB |
Output is correct |
6 |
Correct |
113 ms |
13396 KB |
Output is correct |
7 |
Correct |
110 ms |
13516 KB |
Output is correct |
8 |
Correct |
110 ms |
13520 KB |
Output is correct |
9 |
Correct |
110 ms |
13512 KB |
Output is correct |
10 |
Correct |
116 ms |
13588 KB |
Output is correct |
11 |
Correct |
124 ms |
13480 KB |
Output is correct |
12 |
Correct |
228 ms |
19436 KB |
Output is correct |
13 |
Correct |
108 ms |
13516 KB |
Output is correct |
14 |
Correct |
91 ms |
28400 KB |
Output is correct |
15 |
Correct |
91 ms |
28428 KB |
Output is correct |
16 |
Correct |
218 ms |
19152 KB |
Output is correct |
17 |
Correct |
225 ms |
18640 KB |
Output is correct |
18 |
Correct |
206 ms |
19024 KB |
Output is correct |
19 |
Correct |
114 ms |
22860 KB |
Output is correct |
20 |
Correct |
116 ms |
22860 KB |
Output is correct |
21 |
Correct |
122 ms |
22860 KB |
Output is correct |
22 |
Correct |
115 ms |
22864 KB |
Output is correct |
23 |
Correct |
275 ms |
19440 KB |
Output is correct |
24 |
Correct |
136 ms |
22860 KB |
Output is correct |
25 |
Correct |
110 ms |
13480 KB |
Output is correct |
26 |
Correct |
111 ms |
13508 KB |
Output is correct |
27 |
Correct |
111 ms |
13516 KB |
Output is correct |
28 |
Correct |
112 ms |
13536 KB |
Output is correct |
29 |
Correct |
113 ms |
13520 KB |
Output is correct |
30 |
Correct |
110 ms |
13536 KB |
Output is correct |
31 |
Correct |
123 ms |
13772 KB |
Output is correct |
32 |
Correct |
112 ms |
13464 KB |
Output is correct |
33 |
Correct |
111 ms |
13644 KB |
Output is correct |
34 |
Correct |
257 ms |
19100 KB |
Output is correct |
35 |
Correct |
115 ms |
13640 KB |
Output is correct |
36 |
Correct |
84 ms |
20556 KB |
Output is correct |
37 |
Correct |
90 ms |
20432 KB |
Output is correct |
38 |
Correct |
257 ms |
19324 KB |
Output is correct |
39 |
Correct |
252 ms |
19040 KB |
Output is correct |
40 |
Correct |
269 ms |
19404 KB |
Output is correct |
41 |
Correct |
144 ms |
22936 KB |
Output is correct |
42 |
Correct |
137 ms |
22876 KB |
Output is correct |
43 |
Correct |
132 ms |
22864 KB |
Output is correct |
44 |
Correct |
132 ms |
22860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
335 ms |
24896 KB |
Output is correct |
18 |
Correct |
348 ms |
25144 KB |
Output is correct |
19 |
Correct |
330 ms |
24780 KB |
Output is correct |
20 |
Correct |
333 ms |
24652 KB |
Output is correct |
21 |
Correct |
335 ms |
24844 KB |
Output is correct |
22 |
Correct |
344 ms |
25468 KB |
Output is correct |
23 |
Correct |
341 ms |
25312 KB |
Output is correct |
24 |
Correct |
383 ms |
25456 KB |
Output is correct |
25 |
Correct |
370 ms |
25336 KB |
Output is correct |
26 |
Correct |
351 ms |
25004 KB |
Output is correct |
27 |
Correct |
358 ms |
25008 KB |
Output is correct |
28 |
Correct |
339 ms |
25332 KB |
Output is correct |
29 |
Correct |
323 ms |
24904 KB |
Output is correct |
30 |
Correct |
320 ms |
25072 KB |
Output is correct |
31 |
Correct |
337 ms |
24904 KB |
Output is correct |
32 |
Correct |
328 ms |
25036 KB |
Output is correct |
33 |
Correct |
240 ms |
19444 KB |
Output is correct |
34 |
Correct |
117 ms |
22700 KB |
Output is correct |
35 |
Correct |
111 ms |
13404 KB |
Output is correct |
36 |
Correct |
108 ms |
13444 KB |
Output is correct |
37 |
Correct |
108 ms |
13516 KB |
Output is correct |
38 |
Correct |
113 ms |
13396 KB |
Output is correct |
39 |
Correct |
110 ms |
13516 KB |
Output is correct |
40 |
Correct |
110 ms |
13520 KB |
Output is correct |
41 |
Correct |
110 ms |
13512 KB |
Output is correct |
42 |
Correct |
116 ms |
13588 KB |
Output is correct |
43 |
Correct |
124 ms |
13480 KB |
Output is correct |
44 |
Correct |
228 ms |
19436 KB |
Output is correct |
45 |
Correct |
108 ms |
13516 KB |
Output is correct |
46 |
Correct |
91 ms |
28400 KB |
Output is correct |
47 |
Correct |
91 ms |
28428 KB |
Output is correct |
48 |
Correct |
218 ms |
19152 KB |
Output is correct |
49 |
Correct |
225 ms |
18640 KB |
Output is correct |
50 |
Correct |
206 ms |
19024 KB |
Output is correct |
51 |
Correct |
114 ms |
22860 KB |
Output is correct |
52 |
Correct |
116 ms |
22860 KB |
Output is correct |
53 |
Correct |
122 ms |
22860 KB |
Output is correct |
54 |
Correct |
115 ms |
22864 KB |
Output is correct |
55 |
Correct |
275 ms |
19440 KB |
Output is correct |
56 |
Correct |
136 ms |
22860 KB |
Output is correct |
57 |
Correct |
110 ms |
13480 KB |
Output is correct |
58 |
Correct |
111 ms |
13508 KB |
Output is correct |
59 |
Correct |
111 ms |
13516 KB |
Output is correct |
60 |
Correct |
112 ms |
13536 KB |
Output is correct |
61 |
Correct |
113 ms |
13520 KB |
Output is correct |
62 |
Correct |
110 ms |
13536 KB |
Output is correct |
63 |
Correct |
123 ms |
13772 KB |
Output is correct |
64 |
Correct |
112 ms |
13464 KB |
Output is correct |
65 |
Correct |
111 ms |
13644 KB |
Output is correct |
66 |
Correct |
257 ms |
19100 KB |
Output is correct |
67 |
Correct |
115 ms |
13640 KB |
Output is correct |
68 |
Correct |
84 ms |
20556 KB |
Output is correct |
69 |
Correct |
90 ms |
20432 KB |
Output is correct |
70 |
Correct |
257 ms |
19324 KB |
Output is correct |
71 |
Correct |
252 ms |
19040 KB |
Output is correct |
72 |
Correct |
269 ms |
19404 KB |
Output is correct |
73 |
Correct |
144 ms |
22936 KB |
Output is correct |
74 |
Correct |
137 ms |
22876 KB |
Output is correct |
75 |
Correct |
132 ms |
22864 KB |
Output is correct |
76 |
Correct |
132 ms |
22860 KB |
Output is correct |
77 |
Correct |
343 ms |
26444 KB |
Output is correct |
78 |
Correct |
154 ms |
29604 KB |
Output is correct |
79 |
Correct |
126 ms |
19788 KB |
Output is correct |
80 |
Correct |
127 ms |
19880 KB |
Output is correct |
81 |
Correct |
135 ms |
19908 KB |
Output is correct |
82 |
Correct |
127 ms |
19756 KB |
Output is correct |
83 |
Correct |
125 ms |
19968 KB |
Output is correct |
84 |
Correct |
132 ms |
20024 KB |
Output is correct |
85 |
Correct |
125 ms |
19776 KB |
Output is correct |
86 |
Correct |
127 ms |
19916 KB |
Output is correct |
87 |
Correct |
124 ms |
19916 KB |
Output is correct |
88 |
Correct |
302 ms |
26192 KB |
Output is correct |
89 |
Correct |
126 ms |
19992 KB |
Output is correct |
90 |
Correct |
101 ms |
24780 KB |
Output is correct |
91 |
Correct |
104 ms |
25548 KB |
Output is correct |
92 |
Correct |
314 ms |
26176 KB |
Output is correct |
93 |
Correct |
313 ms |
25812 KB |
Output is correct |
94 |
Correct |
298 ms |
25304 KB |
Output is correct |
95 |
Correct |
150 ms |
29644 KB |
Output is correct |
96 |
Correct |
144 ms |
29644 KB |
Output is correct |
97 |
Correct |
142 ms |
29600 KB |
Output is correct |
98 |
Correct |
147 ms |
29536 KB |
Output is correct |