# include <bits/stdc++.h>
# define fi first
# define se second
# define ll long long
using namespace std;
const int sq = 200;
vector<int> dg[30001];
bitset<30001> jp[30001];
int st, js, ed;
int rc[30001];
bool vis[30001];
ll ch(int a, int b) {
return (1ll * 1e9 * 1ll * a) + (1ll * b);
}
int main() {
int N, M;
scanf("%d %d", &N, &M);
for(int i=0;i<N;i++) rc[i] = 1e9;
for(int i=0;i<M;i++) {
int a, b;
scanf("%d %d", &a, &b);
if(i == 0) {
st = a;
js = b;
} else if(i == 1) ed = a;
else dg[a].push_back(b);
}
queue< pair<pair<int, int>, int> > BFS;
BFS.push(make_pair(make_pair(st, 0), js));
while(BFS.size()) {
int a, b, c;
a = BFS.front().fi.fi;
b = BFS.front().fi.se;
c = BFS.front().se;
// cout<<a<<" "<<b<<" "<
BFS.pop();
rc[a] = min(rc[a], b);
if(a == ed) break;
if(a - c >= 0 && jp[a-c][c] == 0) {
BFS.push(make_pair(make_pair(a - c, b + 1), c));
jp[a-c][c] = 1;
}
if(a + c < N && jp[a+c][c] == 0) {
BFS.push(make_pair(make_pair(a+c, b + 1), c));
jp[a+c][c] = 1;
}
if(vis[a]) continue;
vis[a] = 1;
for(int k=0;k<dg[a].size();k++) {
c = dg[a][k];
if(a - c >= 0 && jp[a-c][c] == 0) {
BFS.push(make_pair(make_pair(a - c, b + 1), c));
jp[a-c][c] = 1;
}
if(a + c < N && jp[a+c][c] == 0) {
BFS.push(make_pair(make_pair(a+c, b + 1), c));
jp[a+c][c] = 1;
}
}
}
if(rc[ed] == 1e9) rc[ed] = -1;
printf("%d\n", rc[ed]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:56:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for(int k=0;k<dg[a].size();k++) {
| ~^~~~~~~~~~~~~
skyscraper.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d %d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1008 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1012 KB |
Output is correct |
17 |
Correct |
2 ms |
3676 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
8364 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
6360 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
4 ms |
7636 KB |
Output is correct |
28 |
Correct |
6 ms |
8404 KB |
Output is correct |
29 |
Correct |
5 ms |
8276 KB |
Output is correct |
30 |
Correct |
5 ms |
8276 KB |
Output is correct |
31 |
Correct |
5 ms |
8276 KB |
Output is correct |
32 |
Correct |
5 ms |
8276 KB |
Output is correct |
33 |
Correct |
11 ms |
8404 KB |
Output is correct |
34 |
Correct |
6 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
3668 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
4 ms |
8404 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
4 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7636 KB |
Output is correct |
28 |
Correct |
5 ms |
8404 KB |
Output is correct |
29 |
Correct |
6 ms |
8276 KB |
Output is correct |
30 |
Correct |
5 ms |
8344 KB |
Output is correct |
31 |
Correct |
5 ms |
8276 KB |
Output is correct |
32 |
Correct |
5 ms |
8276 KB |
Output is correct |
33 |
Correct |
6 ms |
8404 KB |
Output is correct |
34 |
Correct |
6 ms |
8404 KB |
Output is correct |
35 |
Correct |
8 ms |
6740 KB |
Output is correct |
36 |
Correct |
3 ms |
2900 KB |
Output is correct |
37 |
Correct |
8 ms |
8532 KB |
Output is correct |
38 |
Correct |
10 ms |
8844 KB |
Output is correct |
39 |
Correct |
6 ms |
1620 KB |
Output is correct |
40 |
Correct |
6 ms |
3796 KB |
Output is correct |
41 |
Correct |
9 ms |
8020 KB |
Output is correct |
42 |
Correct |
8 ms |
7508 KB |
Output is correct |
43 |
Correct |
8 ms |
7892 KB |
Output is correct |
44 |
Correct |
8 ms |
8592 KB |
Output is correct |
45 |
Correct |
15 ms |
9280 KB |
Output is correct |
46 |
Correct |
12 ms |
9172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
1288 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
3668 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
5 ms |
8404 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
5 ms |
7508 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
4 ms |
7252 KB |
Output is correct |
27 |
Correct |
5 ms |
7636 KB |
Output is correct |
28 |
Correct |
6 ms |
8404 KB |
Output is correct |
29 |
Correct |
5 ms |
8276 KB |
Output is correct |
30 |
Correct |
5 ms |
8276 KB |
Output is correct |
31 |
Correct |
5 ms |
8304 KB |
Output is correct |
32 |
Correct |
5 ms |
8276 KB |
Output is correct |
33 |
Correct |
6 ms |
8404 KB |
Output is correct |
34 |
Correct |
7 ms |
8352 KB |
Output is correct |
35 |
Correct |
8 ms |
6740 KB |
Output is correct |
36 |
Correct |
2 ms |
2900 KB |
Output is correct |
37 |
Correct |
8 ms |
8532 KB |
Output is correct |
38 |
Correct |
10 ms |
8896 KB |
Output is correct |
39 |
Correct |
6 ms |
1620 KB |
Output is correct |
40 |
Correct |
7 ms |
3796 KB |
Output is correct |
41 |
Correct |
9 ms |
8020 KB |
Output is correct |
42 |
Correct |
7 ms |
7508 KB |
Output is correct |
43 |
Correct |
8 ms |
7908 KB |
Output is correct |
44 |
Correct |
8 ms |
8532 KB |
Output is correct |
45 |
Correct |
15 ms |
9180 KB |
Output is correct |
46 |
Correct |
12 ms |
9292 KB |
Output is correct |
47 |
Correct |
22 ms |
31700 KB |
Output is correct |
48 |
Correct |
6 ms |
1492 KB |
Output is correct |
49 |
Correct |
5 ms |
1492 KB |
Output is correct |
50 |
Correct |
4 ms |
1492 KB |
Output is correct |
51 |
Correct |
51 ms |
83024 KB |
Output is correct |
52 |
Correct |
57 ms |
90712 KB |
Output is correct |
53 |
Correct |
16 ms |
17364 KB |
Output is correct |
54 |
Correct |
31 ms |
61264 KB |
Output is correct |
55 |
Correct |
39 ms |
77928 KB |
Output is correct |
56 |
Correct |
59 ms |
112144 KB |
Output is correct |
57 |
Correct |
2 ms |
2772 KB |
Output is correct |
58 |
Correct |
58 ms |
111440 KB |
Output is correct |
59 |
Correct |
56 ms |
103696 KB |
Output is correct |
60 |
Correct |
56 ms |
103672 KB |
Output is correct |
61 |
Correct |
53 ms |
98632 KB |
Output is correct |
62 |
Correct |
90 ms |
112196 KB |
Output is correct |
63 |
Correct |
122 ms |
111736 KB |
Output is correct |
64 |
Correct |
134 ms |
111612 KB |
Output is correct |
65 |
Correct |
162 ms |
111792 KB |
Output is correct |
66 |
Correct |
252 ms |
112192 KB |
Output is correct |
67 |
Correct |
157 ms |
112088 KB |
Output is correct |