#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define forn(i, n) for(int i=0; i<(int)n; ++i)
#define PB push_back
struct node{
int pos, hop, cost;
bool le, ri;
node(int Pos, int Hop, int Cost, bool Le=true, bool Ri=true) : pos(Pos), hop(Hop), cost(Cost), le(Le), ri(Ri) {}
};
const int MAXN=30010, SQ=180;
int n, m, sq, start, target;
bool done[MAXN];
vector<int> ops[MAXN];
bool len[2][MAXN][SQ];
deque<node> Q;
void add(node v, bool mode){
if(v.hop>=sq || !len[mode][v.pos][v.hop]){
if(v.hop<sq) len[mode][v.pos][v.hop]=true;
if(mode) Q.push_back(v);
else Q.push_front(v);
}
}
int main(){
scanf("%d %d", &n, &m);
while((sq+1)*(sq+1)<=n) ++sq;
forn(i, m){
int a, b; scanf("%d %d", &a, &b);
ops[a].PB(b);
if(i==0) start=a;
if(i==1) target=a;
}
for(int doge:ops[start]) add(node(start, doge, 0), false);
int ans=-1;
while(!Q.empty()){
node v = Q.front();
Q.pop_front();
if(v.pos==target){
ans=v.cost;
break;
}
int le=v.pos-v.hop, ri=v.pos+v.hop;
if(le>=0 && v.le) add(node(le, v.hop, v.cost+1, true, false), true);
if(ri<n && v.ri) add(node(ri, v.hop, v.cost+1, false, true), true);
if(!done[v.pos]){
for(int wi:ops[v.pos]) add(node(v.pos, wi, v.cost), false);
done[v.pos]=true;
}
}
printf("%d\n", ans);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:34:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | int a, b; 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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
2 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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1024 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
1032 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1492 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
4 ms |
1364 KB |
Output is correct |
34 |
Correct |
3 ms |
1304 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 |
940 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1240 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
2 ms |
1492 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
3 ms |
1364 KB |
Output is correct |
30 |
Correct |
2 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
4 ms |
1364 KB |
Output is correct |
34 |
Correct |
3 ms |
1236 KB |
Output is correct |
35 |
Correct |
7 ms |
1876 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
6 ms |
2132 KB |
Output is correct |
38 |
Correct |
8 ms |
1996 KB |
Output is correct |
39 |
Correct |
6 ms |
1244 KB |
Output is correct |
40 |
Correct |
8 ms |
1708 KB |
Output is correct |
41 |
Correct |
8 ms |
2260 KB |
Output is correct |
42 |
Correct |
5 ms |
1548 KB |
Output is correct |
43 |
Correct |
6 ms |
1604 KB |
Output is correct |
44 |
Correct |
5 ms |
1876 KB |
Output is correct |
45 |
Correct |
13 ms |
2044 KB |
Output is correct |
46 |
Correct |
13 ms |
1748 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 |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
964 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
2 ms |
1748 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
1492 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1364 KB |
Output is correct |
29 |
Correct |
2 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
1 ms |
1364 KB |
Output is correct |
33 |
Correct |
4 ms |
1368 KB |
Output is correct |
34 |
Correct |
2 ms |
1236 KB |
Output is correct |
35 |
Correct |
7 ms |
1876 KB |
Output is correct |
36 |
Correct |
2 ms |
1108 KB |
Output is correct |
37 |
Correct |
9 ms |
2136 KB |
Output is correct |
38 |
Correct |
7 ms |
1992 KB |
Output is correct |
39 |
Correct |
6 ms |
1236 KB |
Output is correct |
40 |
Correct |
7 ms |
1708 KB |
Output is correct |
41 |
Correct |
8 ms |
2356 KB |
Output is correct |
42 |
Correct |
5 ms |
1620 KB |
Output is correct |
43 |
Correct |
7 ms |
1652 KB |
Output is correct |
44 |
Correct |
7 ms |
1876 KB |
Output is correct |
45 |
Correct |
16 ms |
2004 KB |
Output is correct |
46 |
Correct |
11 ms |
1748 KB |
Output is correct |
47 |
Correct |
9 ms |
4692 KB |
Output is correct |
48 |
Correct |
6 ms |
1364 KB |
Output is correct |
49 |
Correct |
7 ms |
1352 KB |
Output is correct |
50 |
Correct |
5 ms |
1364 KB |
Output is correct |
51 |
Correct |
13 ms |
4752 KB |
Output is correct |
52 |
Correct |
13 ms |
5076 KB |
Output is correct |
53 |
Correct |
10 ms |
2388 KB |
Output is correct |
54 |
Correct |
4 ms |
6228 KB |
Output is correct |
55 |
Correct |
5 ms |
6216 KB |
Output is correct |
56 |
Correct |
19 ms |
12424 KB |
Output is correct |
57 |
Correct |
1 ms |
1364 KB |
Output is correct |
58 |
Correct |
10 ms |
6484 KB |
Output is correct |
59 |
Correct |
11 ms |
6604 KB |
Output is correct |
60 |
Correct |
10 ms |
6612 KB |
Output is correct |
61 |
Correct |
10 ms |
6612 KB |
Output is correct |
62 |
Correct |
26 ms |
7296 KB |
Output is correct |
63 |
Correct |
40 ms |
6732 KB |
Output is correct |
64 |
Correct |
43 ms |
5204 KB |
Output is correct |
65 |
Correct |
110 ms |
7004 KB |
Output is correct |
66 |
Correct |
251 ms |
7232 KB |
Output is correct |
67 |
Correct |
108 ms |
5600 KB |
Output is correct |