#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#define N 30000
#define INF 0x3f3f3f3f
unsigned int X;
void srand_() {
struct timeval tv;
gettimeofday(&tv, NULL);
X = tv.tv_sec ^ tv.tv_usec | 1;
}
int rand_() {
return (X *= 3) >> 1;
}
int *ed[N], eo[N], *ej[N], *ew[N], eo_[N];
void append(int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ed[i] = (int *) realloc(ed[i], o * 2 * sizeof *ed[i]);
ed[i][o] = j;
}
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;
}
void sort(int *aa, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, a_ = aa[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = aa[j] - a_;
if (c == 0)
j++;
else if (c < 0) {
tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
i++, j++;
} else {
k--;
tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
}
}
sort(aa, l, i);
l = k;
}
}
int find(int i, int d) {
int lower = 0, upper = eo[i] - 1;
while (lower < upper) {
int t = (lower + upper) / 2;
if (ed[i][t] < d)
lower = t + 1;
else if (ed[i][t] > d)
upper = t - 1;
else
return 1;
}
return 0;
}
int dd[N], pq[1 + N], iq[N], cnt;
int lt(int u, int v) {
return dd[u] < dd[v];
}
int i2(int i) {
return (i *= 2) > cnt ? 0 : i < cnt && lt(pq[i + 1], pq[i]) ? i + 1 : i;
}
void pq_up(int u) {
int i, j;
for (i = iq[u]; (j = i / 2) && lt(u, pq[j]); i = j)
pq[iq[pq[j]] = i] = pq[j];
pq[iq[u] = i] = u;
}
void pq_dn(int u) {
int i, j;
for (i = iq[u]; (j = i2(i)) && lt(pq[j], u); i = j)
pq[iq[pq[j]] = i] = pq[j];
pq[iq[u] = i] = u;
}
void pq_add(int u) {
iq[u] = ++cnt;
pq_up(u);
}
int pq_remove_first() {
int u = pq[1], v = pq[cnt--];
iq[v] = 1;
pq_dn(v);
return u;
}
int main() {
int n, m, i, j, d, s, t;
srand_();
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
ed[i] = (int *) malloc(2 * sizeof *ed[i]), eo[i] = 0;
ej[i] = (int *) malloc(2 * sizeof *ej[i]), ew[i] = (int *) malloc(2 * sizeof *ew[i]), eo_[i] = 0;
}
s = t = -1;
for (i = 0; i < m; i++) {
scanf("%d%d", &j, &d);
if (i == 0)
s = j;
else if (i == 1)
t = j;
append(j, d);
}
for (i = 0; i < n; i++)
sort(ed[i], 0, eo[i]);
for (i = 0; i < n; i++) {
int o;
for (o = eo[i]; o--; ) {
d = ed[i][o];
for (j = i + d; j < n; j += d) {
append_(i, j, (j - i) / d);
if (find(j, d))
break;
}
for (j = i - d; j >= 0; j -= d) {
append_(i, j, (i - j) / d);
if (find(j, d))
break;
}
}
}
memset(dd, 0x3f, n * sizeof *dd);
dd[s] = 0;
pq_add(s);
while (cnt) {
int o;
i = pq_remove_first();
for (o = eo_[i]; o--; ) {
int j = ej[i][o], w = ew[i][o];
d = dd[i] + w;
if (dd[j] == INF) {
dd[j] = d;
pq_add(j);
} else if (d < dd[j]) {
dd[j] = d;
pq_up(j);
}
}
}
printf("%d\n", dd[t] == INF ? -1 : dd[t]);
for (i = 0; i < n; i++) {
free(ed[i]);
free(ej[i]);
free(ew[i]);
}
}
Compilation message
skyscraper.c: In function 'srand_':
skyscraper.c:15:16: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
15 | X = tv.tv_sec ^ tv.tv_usec | 1;
| ~~~~~~~~~~^~~~~~~~~~~~
skyscraper.c: In function 'append':
skyscraper.c:27:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
27 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
skyscraper.c: In function 'append_':
skyscraper.c:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
35 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:123:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:130:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | scanf("%d%d", &j, &d);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
5 ms |
1888 KB |
Output is correct |
13 |
Correct |
3 ms |
2016 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
1888 KB |
Output is correct |
13 |
Correct |
4 ms |
2016 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
488 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
50 ms |
32708 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
40 ms |
28904 KB |
Output is correct |
27 |
Correct |
39 ms |
27336 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
4 ms |
1740 KB |
Output is correct |
34 |
Correct |
4 ms |
1740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
1888 KB |
Output is correct |
13 |
Correct |
3 ms |
1952 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
588 KB |
Output is correct |
18 |
Correct |
2 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
53 ms |
32624 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
41 ms |
28952 KB |
Output is correct |
27 |
Correct |
43 ms |
27212 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
716 KB |
Output is correct |
33 |
Correct |
4 ms |
1792 KB |
Output is correct |
34 |
Correct |
3 ms |
1740 KB |
Output is correct |
35 |
Correct |
10 ms |
1628 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
12 ms |
2816 KB |
Output is correct |
38 |
Correct |
14 ms |
2636 KB |
Output is correct |
39 |
Correct |
15 ms |
2688 KB |
Output is correct |
40 |
Correct |
16 ms |
2792 KB |
Output is correct |
41 |
Correct |
15 ms |
2636 KB |
Output is correct |
42 |
Runtime error |
316 ms |
262148 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
3 ms |
2004 KB |
Output is correct |
13 |
Correct |
3 ms |
2016 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
54 ms |
32712 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
44 ms |
28948 KB |
Output is correct |
27 |
Correct |
39 ms |
27376 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
2 ms |
1100 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
2 ms |
844 KB |
Output is correct |
33 |
Correct |
4 ms |
1740 KB |
Output is correct |
34 |
Correct |
3 ms |
1808 KB |
Output is correct |
35 |
Correct |
14 ms |
1740 KB |
Output is correct |
36 |
Correct |
3 ms |
588 KB |
Output is correct |
37 |
Correct |
18 ms |
2892 KB |
Output is correct |
38 |
Correct |
13 ms |
2580 KB |
Output is correct |
39 |
Correct |
14 ms |
2636 KB |
Output is correct |
40 |
Correct |
18 ms |
2700 KB |
Output is correct |
41 |
Correct |
14 ms |
2636 KB |
Output is correct |
42 |
Runtime error |
324 ms |
262148 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |