#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define M 200000
#define INF 0x3f3f3f3f
int max(int a, int b) { return a > b ? a : b; }
int *eh[N], eo[N], *fh[N], fo[N], dd[N], n;
int ii[M], jj[M], ww[M], xx[M], yy[M], m;
void append(int **eh, int *eo, 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;
}
int pq[N + M], *iq[N + 1], cnt[N + 1], mode;
int (*compare)(int, int);
int lt(int i, int j) {
return mode < n ? xx[i - n] < xx[j - n] : dd[i] > dd[j];
}
int p2(int p) {
return (p *= 2) > cnt[mode] ? 0 : (p < cnt[mode] && lt(iq[mode][p + 1], iq[mode][p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[mode][q]); p = q)
iq[mode][pq[j] = p] = j;
iq[mode][pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[mode][q], i); p = q)
iq[mode][pq[j] = p] = j;
iq[mode][pq[i] = p] = i;
}
void pq_add(int i) {
pq[i] = ++cnt[mode], pq_up(i);
}
int pq_first() {
return iq[mode][1];
}
int pq_remove_first() {
int i = iq[mode][1], j = iq[mode][cnt[mode]--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int main() {
int h, i, j, x, y, o;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
fh[i] = (int *) malloc(2 * sizeof *fh[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d%d%d", &i, &j, &x, &y), i--, j--;
ii[h] = i, jj[h] = j, xx[h] = x, yy[h] = y;
append(eh, eo, i, h), append(fh, fo, j, h);
}
for (i = 0; i < n; i++) {
mode = i;
iq[mode] = (int *) malloc((eo[i] + 1) * sizeof *iq[mode]);
for (o = eo[i]; o--; ) {
h = eh[i][o];
pq_add(n + h);
}
dd[i] = cnt[i] == 0 ? INF : xx[pq_first() - n];
}
mode = n;
iq[mode] = (int *) malloc((n + 1) * sizeof *iq[mode]);
for (i = 0; i < n; i++)
pq_add(i);
while (cnt[mode = n]) {
j = pq_remove_first();
for (o = fo[j]; o--; ) {
h = fh[j][o], i = ii[h];
xx[h] = max(xx[h], dd[j] == INF ? INF : dd[j] - yy[h]);
mode = i, pq_dn(n + h);
dd[i] = cnt[i] == 0 ? INF : xx[pq_first() - n];
mode = n, pq_up(i);
}
}
for (i = 0; i < n; i++)
printf("%d ", dd[i] == INF ? -1 : dd[i]);
printf("\n");
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:75:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d%d%d%d", &i, &j, &x, &y), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
32996 KB |
Output is correct |
2 |
Correct |
216 ms |
32916 KB |
Output is correct |
3 |
Correct |
62 ms |
7680 KB |
Output is correct |
4 |
Correct |
46 ms |
29596 KB |
Output is correct |
5 |
Correct |
154 ms |
24216 KB |
Output is correct |
6 |
Correct |
157 ms |
24244 KB |
Output is correct |
7 |
Correct |
70 ms |
11172 KB |
Output is correct |
8 |
Correct |
247 ms |
39240 KB |
Output is correct |
9 |
Correct |
255 ms |
38128 KB |
Output is correct |
10 |
Correct |
61 ms |
10288 KB |
Output is correct |
11 |
Correct |
146 ms |
22696 KB |
Output is correct |
12 |
Correct |
55 ms |
9100 KB |
Output is correct |
13 |
Correct |
51 ms |
8320 KB |
Output is correct |
14 |
Correct |
262 ms |
39144 KB |
Output is correct |
15 |
Correct |
253 ms |
39148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
480 KB |
Output is correct |
6 |
Correct |
1 ms |
564 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
688 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
284 ms |
32996 KB |
Output is correct |
16 |
Correct |
216 ms |
32916 KB |
Output is correct |
17 |
Correct |
62 ms |
7680 KB |
Output is correct |
18 |
Correct |
46 ms |
29596 KB |
Output is correct |
19 |
Correct |
154 ms |
24216 KB |
Output is correct |
20 |
Correct |
157 ms |
24244 KB |
Output is correct |
21 |
Correct |
70 ms |
11172 KB |
Output is correct |
22 |
Correct |
247 ms |
39240 KB |
Output is correct |
23 |
Correct |
255 ms |
38128 KB |
Output is correct |
24 |
Correct |
61 ms |
10288 KB |
Output is correct |
25 |
Correct |
146 ms |
22696 KB |
Output is correct |
26 |
Correct |
55 ms |
9100 KB |
Output is correct |
27 |
Correct |
51 ms |
8320 KB |
Output is correct |
28 |
Correct |
262 ms |
39144 KB |
Output is correct |
29 |
Correct |
253 ms |
39148 KB |
Output is correct |
30 |
Correct |
260 ms |
37768 KB |
Output is correct |
31 |
Correct |
223 ms |
37748 KB |
Output is correct |
32 |
Correct |
64 ms |
10444 KB |
Output is correct |
33 |
Correct |
40 ms |
29560 KB |
Output is correct |
34 |
Correct |
181 ms |
24268 KB |
Output is correct |
35 |
Correct |
139 ms |
24264 KB |
Output is correct |
36 |
Correct |
68 ms |
11216 KB |
Output is correct |
37 |
Correct |
249 ms |
39300 KB |
Output is correct |
38 |
Correct |
227 ms |
39116 KB |
Output is correct |
39 |
Correct |
58 ms |
10388 KB |
Output is correct |
40 |
Correct |
147 ms |
22732 KB |
Output is correct |
41 |
Correct |
58 ms |
9196 KB |
Output is correct |
42 |
Correct |
53 ms |
8268 KB |
Output is correct |
43 |
Correct |
254 ms |
39124 KB |
Output is correct |
44 |
Correct |
253 ms |
39220 KB |
Output is correct |
45 |
Correct |
253 ms |
39144 KB |
Output is correct |
46 |
Correct |
127 ms |
25172 KB |
Output is correct |
47 |
Correct |
130 ms |
23256 KB |
Output is correct |
48 |
Correct |
115 ms |
23628 KB |
Output is correct |
49 |
Correct |
179 ms |
23548 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |