#include <stdio.h>
#include <stdlib.h>
#define N 1000000
int *ej[N], *ew[N], eo[N];
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
int aa[N], iq[N + 1], pq[N], cnt;
int lt(int i, int j) { return aa[i] > aa[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;
}
int main() {
int n, m, h, i, j, o, w;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d%d", &i, &j, &w), i--, j--;
append(i, j, w), append(j, i, w);
}
for (i = 0; i < n; i++)
pq_add_last(i);
for (h = cnt / 2; h > 0; h--)
pq_dn(iq[h]);
while (cnt) {
i = pq_remove_first();
for (o = eo[i]; o--; ) {
j = ej[i][o], w = ew[i][o];
if (aa[j] < aa[i] - w)
aa[j] = aa[i] - w, pq_up(j);
}
}
for (i = 0; i < n; i++)
printf("%d\n", aa[i]);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:58:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:60:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
Main.c:66:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d%d", &i, &j, &w), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8880 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8744 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
971 ms |
130900 KB |
Output is correct |
2 |
Correct |
1002 ms |
130456 KB |
Output is correct |
3 |
Correct |
996 ms |
130632 KB |
Output is correct |
4 |
Correct |
933 ms |
130384 KB |
Output is correct |
5 |
Correct |
926 ms |
121168 KB |
Output is correct |
6 |
Correct |
870 ms |
121936 KB |
Output is correct |
7 |
Correct |
757 ms |
112976 KB |
Output is correct |
8 |
Correct |
827 ms |
113304 KB |
Output is correct |
9 |
Correct |
748 ms |
105608 KB |
Output is correct |
10 |
Correct |
797 ms |
106000 KB |
Output is correct |
11 |
Correct |
652 ms |
99216 KB |
Output is correct |
12 |
Correct |
691 ms |
99220 KB |
Output is correct |
13 |
Correct |
627 ms |
89164 KB |
Output is correct |
14 |
Correct |
650 ms |
89172 KB |
Output is correct |
15 |
Correct |
319 ms |
55636 KB |
Output is correct |
16 |
Correct |
321 ms |
55632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1097 ms |
138036 KB |
Output is correct |
2 |
Correct |
1057 ms |
138504 KB |
Output is correct |
3 |
Correct |
992 ms |
137812 KB |
Output is correct |
4 |
Correct |
1016 ms |
137996 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1082 ms |
135056 KB |
Output is correct |
8 |
Correct |
1061 ms |
133972 KB |
Output is correct |
9 |
Correct |
1000 ms |
130472 KB |
Output is correct |
10 |
Correct |
1107 ms |
131080 KB |
Output is correct |
11 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
3 ms |
8880 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8540 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8792 KB |
Output is correct |
10 |
Correct |
2 ms |
8744 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
1 ms |
8540 KB |
Output is correct |
14 |
Correct |
1 ms |
8540 KB |
Output is correct |
15 |
Correct |
971 ms |
130900 KB |
Output is correct |
16 |
Correct |
1002 ms |
130456 KB |
Output is correct |
17 |
Correct |
996 ms |
130632 KB |
Output is correct |
18 |
Correct |
933 ms |
130384 KB |
Output is correct |
19 |
Correct |
926 ms |
121168 KB |
Output is correct |
20 |
Correct |
870 ms |
121936 KB |
Output is correct |
21 |
Correct |
757 ms |
112976 KB |
Output is correct |
22 |
Correct |
827 ms |
113304 KB |
Output is correct |
23 |
Correct |
748 ms |
105608 KB |
Output is correct |
24 |
Correct |
797 ms |
106000 KB |
Output is correct |
25 |
Correct |
652 ms |
99216 KB |
Output is correct |
26 |
Correct |
691 ms |
99220 KB |
Output is correct |
27 |
Correct |
627 ms |
89164 KB |
Output is correct |
28 |
Correct |
650 ms |
89172 KB |
Output is correct |
29 |
Correct |
319 ms |
55636 KB |
Output is correct |
30 |
Correct |
321 ms |
55632 KB |
Output is correct |
31 |
Correct |
1097 ms |
138036 KB |
Output is correct |
32 |
Correct |
1057 ms |
138504 KB |
Output is correct |
33 |
Correct |
992 ms |
137812 KB |
Output is correct |
34 |
Correct |
1016 ms |
137996 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8540 KB |
Output is correct |
37 |
Correct |
1082 ms |
135056 KB |
Output is correct |
38 |
Correct |
1061 ms |
133972 KB |
Output is correct |
39 |
Correct |
1000 ms |
130472 KB |
Output is correct |
40 |
Correct |
1107 ms |
131080 KB |
Output is correct |
41 |
Correct |
1 ms |
8540 KB |
Output is correct |
42 |
Correct |
1102 ms |
138580 KB |
Output is correct |
43 |
Correct |
1002 ms |
138324 KB |
Output is correct |
44 |
Correct |
985 ms |
130392 KB |
Output is correct |
45 |
Correct |
1098 ms |
131148 KB |
Output is correct |
46 |
Correct |
1067 ms |
132244 KB |
Output is correct |
47 |
Correct |
1039 ms |
129620 KB |
Output is correct |
48 |
Correct |
924 ms |
122172 KB |
Output is correct |
49 |
Correct |
903 ms |
120796 KB |
Output is correct |
50 |
Correct |
919 ms |
115792 KB |
Output is correct |
51 |
Correct |
308 ms |
56604 KB |
Output is correct |
52 |
Correct |
339 ms |
55856 KB |
Output is correct |
53 |
Correct |
210 ms |
53328 KB |
Output is correct |
54 |
Correct |
202 ms |
49544 KB |
Output is correct |
55 |
Correct |
1102 ms |
133200 KB |
Output is correct |
56 |
Correct |
1071 ms |
132200 KB |
Output is correct |
57 |
Correct |
417 ms |
101760 KB |
Output is correct |