#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
int min(int a, int b) { return a < b ? a : b; }
#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];
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;
}
struct L {
struct L *next;
int j, w;
} *ej[N];
void link(int i, int j, int w) {
struct L *l = (struct L *) calloc(1, sizeof *l);
l->j = j, l->w = w;
l->next = ej[i];
ej[i] = l;
};
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 *aa, int n, int a) {
int lower = 0, upper = n - 1;
while (lower < upper) {
int t = (lower + upper) / 2;
if (aa[t] < a)
lower = t + 1;
else if (aa[t] > a)
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() {
static int jj[N], ww[N];
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;
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]);
memset(ww, -1, n * sizeof *ww);
for (i = 0; i < n; i++) {
int o, o_;
o_ = 0;
for (o = eo[i]; o--; ) {
d = ed[i][o];
if (o + 1 < eo[i] && d == ed[i][o + 1])
continue;
for (j = i + d; j < n; j += d) {
if (ww[j] == -1) {
jj[o_++] = j;
ww[j] = (j - i) / d;
} else
ww[j] = min(ww[j], (j - i) / d);
if (find(ed[j], eo[j], d))
break;
}
for (j = i - d; j >= 0; j -= d) {
if (ww[j] == -1) {
jj[o_++] = j;
ww[j] = (i - j) / d;
} else
ww[j] = min(ww[j], (i - j) / d);
if (find(ed[j], eo[j], d))
break;
}
}
for (o = 0; o < o_; o++) {
j = jj[o];
link(i, j, ww[j]);
ww[j] = -1;
}
}
memset(dd, 0x3f, n * sizeof *dd);
dd[s] = 0;
pq_add(s);
while (cnt) {
struct L *l;
i = pq_remove_first();
for (l = ej[i]; l; l = l->next) {
d = dd[i] + l->w;
if (dd[l->j] == INF) {
dd[l->j] = d;
pq_add(l->j);
} else if (d < dd[l->j]) {
dd[l->j] = d;
pq_up(l->j);
}
}
}
printf("%d\n", dd[t] == INF ? -1 : dd[t]);
}
Compilation message
skyscraper.c: In function 'srand_':
skyscraper.c:17:16: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
17 | X = tv.tv_sec ^ tv.tv_usec | 1;
| ~~~~~~~~~~^~~~~~~~~~~~
skyscraper.c: In function 'append':
skyscraper.c:29:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
29 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:129:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:134:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | scanf("%d%d", &j, &d);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
264 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
264 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 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 |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
292 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
288 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
292 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
292 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
292 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
188 ms |
125528 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 |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
584 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
588 KB |
Output is correct |
28 |
Correct |
3 ms |
1100 KB |
Output is correct |
29 |
Correct |
3 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
844 KB |
Output is correct |
31 |
Correct |
3 ms |
1228 KB |
Output is correct |
32 |
Correct |
3 ms |
1100 KB |
Output is correct |
33 |
Correct |
6 ms |
1996 KB |
Output is correct |
34 |
Correct |
4 ms |
1996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
252 KB |
Output is correct |
3 |
Correct |
0 ms |
284 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
268 KB |
Output is correct |
11 |
Correct |
1 ms |
428 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
588 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 |
844 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
548 KB |
Output is correct |
20 |
Correct |
194 ms |
125576 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 |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
588 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
660 KB |
Output is correct |
28 |
Correct |
2 ms |
1100 KB |
Output is correct |
29 |
Correct |
3 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
844 KB |
Output is correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Correct |
2 ms |
1100 KB |
Output is correct |
33 |
Correct |
4 ms |
1996 KB |
Output is correct |
34 |
Correct |
4 ms |
1960 KB |
Output is correct |
35 |
Correct |
18 ms |
3604 KB |
Output is correct |
36 |
Correct |
4 ms |
844 KB |
Output is correct |
37 |
Correct |
17 ms |
6660 KB |
Output is correct |
38 |
Correct |
19 ms |
5956 KB |
Output is correct |
39 |
Correct |
26 ms |
6124 KB |
Output is correct |
40 |
Correct |
20 ms |
6328 KB |
Output is correct |
41 |
Correct |
19 ms |
5964 KB |
Output is correct |
42 |
Correct |
7 ms |
972 KB |
Output is correct |
43 |
Correct |
7 ms |
972 KB |
Output is correct |
44 |
Correct |
186 ms |
125784 KB |
Output is correct |
45 |
Correct |
18 ms |
7524 KB |
Output is correct |
46 |
Correct |
19 ms |
7492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
292 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
284 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
448 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
404 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
420 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
168 ms |
125628 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
716 KB |
Output is correct |
25 |
Correct |
2 ms |
708 KB |
Output is correct |
26 |
Correct |
2 ms |
676 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
3 ms |
1068 KB |
Output is correct |
29 |
Correct |
2 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
928 KB |
Output is correct |
31 |
Correct |
2 ms |
1184 KB |
Output is correct |
32 |
Correct |
2 ms |
1060 KB |
Output is correct |
33 |
Correct |
4 ms |
1936 KB |
Output is correct |
34 |
Correct |
4 ms |
1996 KB |
Output is correct |
35 |
Correct |
13 ms |
3608 KB |
Output is correct |
36 |
Correct |
3 ms |
876 KB |
Output is correct |
37 |
Correct |
17 ms |
6616 KB |
Output is correct |
38 |
Correct |
19 ms |
5960 KB |
Output is correct |
39 |
Correct |
18 ms |
6108 KB |
Output is correct |
40 |
Correct |
19 ms |
6332 KB |
Output is correct |
41 |
Correct |
18 ms |
5872 KB |
Output is correct |
42 |
Correct |
7 ms |
1032 KB |
Output is correct |
43 |
Correct |
7 ms |
888 KB |
Output is correct |
44 |
Correct |
172 ms |
125724 KB |
Output is correct |
45 |
Correct |
19 ms |
7436 KB |
Output is correct |
46 |
Correct |
18 ms |
7492 KB |
Output is correct |
47 |
Correct |
51 ms |
27712 KB |
Output is correct |
48 |
Correct |
17 ms |
8728 KB |
Output is correct |
49 |
Correct |
14 ms |
5836 KB |
Output is correct |
50 |
Correct |
12 ms |
6220 KB |
Output is correct |
51 |
Correct |
35 ms |
13164 KB |
Output is correct |
52 |
Correct |
36 ms |
14492 KB |
Output is correct |
53 |
Correct |
19 ms |
5388 KB |
Output is correct |
54 |
Correct |
5 ms |
2980 KB |
Output is correct |
55 |
Correct |
9 ms |
4752 KB |
Output is correct |
56 |
Runtime error |
446 ms |
262148 KB |
Execution killed with signal 9 |
57 |
Halted |
0 ms |
0 KB |
- |