#include <bits/stdc++.h>
#define va first
#define vb second
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
typedef pair<int,int> pii;
const int mx=30005;
int dist[mx], B[mx], P[mx];
vector<int> C[mx];
int N,M;
void input(){
scanf("%d%d",&N,&M);
for(int i=0;i<M;i++){
scanf("%d%d",&B[i],&P[i]);
C[B[i]].push_back(P[i]);
}
for(int i=0;i<N;i++){
sort(all(C[i]));
C[i].erase(unique(all(C[i])),C[i].end());
}
fill(dist,dist+N,(int)1e9);
}
void dijk(int s){
priority_queue<pii,vector<pii>,greater<pii>> q;
q.push(mp(dist[s]=0,s));
pii t;
while(!q.empty()){
t=q.top(); q.pop();
if(t.va>dist[t.vb]) continue;
for(int p : C[t.vb]){
for(int x=1,o=t.vb+p,c=t.va+1;o<N;++x,o+=p,++c){
if(dist[o]>c){
q.push(mp(dist[o]=c,o));
if(binary_search(all(C[o]),p)) break;
}
}
for(int x=1,o=t.vb-p,c=t.va+1;o>=0;++x,o-=p,++c){
if(dist[o]>c){
q.push(mp(dist[o]=c,o));
if(binary_search(all(C[o]),p)) break;
}
}
}
}
}
int main(){
input();
dijk(B[0]);
printf("%d\n",dist[B[1]]==(int)1e9?-1:dist[B[1]]);
}
Compilation message
skyscraper.cpp: In function 'void input()':
skyscraper.cpp:13:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&N,&M);
^
skyscraper.cpp:15:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&B[i],&P[i]);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3076 KB |
Output is correct |
2 |
Correct |
0 ms |
3076 KB |
Output is correct |
3 |
Correct |
0 ms |
3076 KB |
Output is correct |
4 |
Correct |
0 ms |
3076 KB |
Output is correct |
5 |
Correct |
0 ms |
3076 KB |
Output is correct |
6 |
Correct |
0 ms |
3076 KB |
Output is correct |
7 |
Correct |
0 ms |
3076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3076 KB |
Output is correct |
2 |
Correct |
0 ms |
3076 KB |
Output is correct |
3 |
Correct |
0 ms |
3076 KB |
Output is correct |
4 |
Correct |
0 ms |
3076 KB |
Output is correct |
5 |
Correct |
0 ms |
3076 KB |
Output is correct |
6 |
Correct |
0 ms |
3076 KB |
Output is correct |
7 |
Correct |
0 ms |
3076 KB |
Output is correct |
8 |
Correct |
0 ms |
3076 KB |
Output is correct |
9 |
Correct |
0 ms |
3076 KB |
Output is correct |
10 |
Correct |
0 ms |
3076 KB |
Output is correct |
11 |
Correct |
0 ms |
3076 KB |
Output is correct |
12 |
Correct |
0 ms |
3076 KB |
Output is correct |
13 |
Correct |
0 ms |
3076 KB |
Output is correct |
14 |
Correct |
0 ms |
3076 KB |
Output is correct |
15 |
Correct |
0 ms |
3076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3076 KB |
Output is correct |
2 |
Correct |
0 ms |
3076 KB |
Output is correct |
3 |
Correct |
0 ms |
3076 KB |
Output is correct |
4 |
Correct |
0 ms |
3076 KB |
Output is correct |
5 |
Correct |
0 ms |
3076 KB |
Output is correct |
6 |
Correct |
0 ms |
3076 KB |
Output is correct |
7 |
Correct |
0 ms |
3076 KB |
Output is correct |
8 |
Correct |
0 ms |
3076 KB |
Output is correct |
9 |
Correct |
0 ms |
3076 KB |
Output is correct |
10 |
Correct |
0 ms |
3076 KB |
Output is correct |
11 |
Correct |
0 ms |
3076 KB |
Output is correct |
12 |
Correct |
0 ms |
3076 KB |
Output is correct |
13 |
Correct |
0 ms |
3076 KB |
Output is correct |
14 |
Correct |
0 ms |
3076 KB |
Output is correct |
15 |
Correct |
0 ms |
3076 KB |
Output is correct |
16 |
Correct |
0 ms |
3076 KB |
Output is correct |
17 |
Correct |
0 ms |
3208 KB |
Output is correct |
18 |
Correct |
0 ms |
3076 KB |
Output is correct |
19 |
Correct |
0 ms |
3076 KB |
Output is correct |
20 |
Correct |
0 ms |
3208 KB |
Output is correct |
21 |
Correct |
0 ms |
3076 KB |
Output is correct |
22 |
Correct |
0 ms |
3076 KB |
Output is correct |
23 |
Correct |
0 ms |
3076 KB |
Output is correct |
24 |
Correct |
0 ms |
3220 KB |
Output is correct |
25 |
Correct |
0 ms |
3208 KB |
Output is correct |
26 |
Correct |
0 ms |
3076 KB |
Output is correct |
27 |
Correct |
0 ms |
3076 KB |
Output is correct |
28 |
Correct |
0 ms |
3208 KB |
Output is correct |
29 |
Correct |
3 ms |
3356 KB |
Output is correct |
30 |
Correct |
0 ms |
3220 KB |
Output is correct |
31 |
Correct |
0 ms |
3220 KB |
Output is correct |
32 |
Correct |
0 ms |
3224 KB |
Output is correct |
33 |
Correct |
3 ms |
3560 KB |
Output is correct |
34 |
Correct |
3 ms |
3560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3076 KB |
Output is correct |
2 |
Correct |
0 ms |
3076 KB |
Output is correct |
3 |
Correct |
0 ms |
3076 KB |
Output is correct |
4 |
Correct |
0 ms |
3076 KB |
Output is correct |
5 |
Correct |
0 ms |
3076 KB |
Output is correct |
6 |
Correct |
0 ms |
3076 KB |
Output is correct |
7 |
Correct |
0 ms |
3076 KB |
Output is correct |
8 |
Correct |
0 ms |
3076 KB |
Output is correct |
9 |
Correct |
0 ms |
3076 KB |
Output is correct |
10 |
Correct |
0 ms |
3076 KB |
Output is correct |
11 |
Correct |
0 ms |
3076 KB |
Output is correct |
12 |
Correct |
0 ms |
3076 KB |
Output is correct |
13 |
Correct |
0 ms |
3076 KB |
Output is correct |
14 |
Correct |
0 ms |
3076 KB |
Output is correct |
15 |
Correct |
0 ms |
3076 KB |
Output is correct |
16 |
Correct |
0 ms |
3076 KB |
Output is correct |
17 |
Correct |
0 ms |
3208 KB |
Output is correct |
18 |
Correct |
0 ms |
3076 KB |
Output is correct |
19 |
Correct |
0 ms |
3076 KB |
Output is correct |
20 |
Correct |
3 ms |
3208 KB |
Output is correct |
21 |
Correct |
0 ms |
3076 KB |
Output is correct |
22 |
Correct |
0 ms |
3076 KB |
Output is correct |
23 |
Correct |
0 ms |
3076 KB |
Output is correct |
24 |
Correct |
0 ms |
3220 KB |
Output is correct |
25 |
Correct |
0 ms |
3208 KB |
Output is correct |
26 |
Correct |
0 ms |
3076 KB |
Output is correct |
27 |
Correct |
0 ms |
3076 KB |
Output is correct |
28 |
Correct |
0 ms |
3208 KB |
Output is correct |
29 |
Correct |
3 ms |
3356 KB |
Output is correct |
30 |
Correct |
0 ms |
3220 KB |
Output is correct |
31 |
Correct |
0 ms |
3220 KB |
Output is correct |
32 |
Correct |
0 ms |
3224 KB |
Output is correct |
33 |
Correct |
3 ms |
3560 KB |
Output is correct |
34 |
Correct |
6 ms |
3560 KB |
Output is correct |
35 |
Correct |
9 ms |
3340 KB |
Output is correct |
36 |
Correct |
0 ms |
3208 KB |
Output is correct |
37 |
Correct |
9 ms |
3344 KB |
Output is correct |
38 |
Correct |
13 ms |
3524 KB |
Output is correct |
39 |
Correct |
6 ms |
3520 KB |
Output is correct |
40 |
Correct |
19 ms |
3524 KB |
Output is correct |
41 |
Correct |
9 ms |
3524 KB |
Output is correct |
42 |
Correct |
9 ms |
3352 KB |
Output is correct |
43 |
Correct |
13 ms |
3352 KB |
Output is correct |
44 |
Correct |
13 ms |
3348 KB |
Output is correct |
45 |
Correct |
13 ms |
3720 KB |
Output is correct |
46 |
Correct |
9 ms |
3724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
3076 KB |
Output is correct |
2 |
Correct |
0 ms |
3076 KB |
Output is correct |
3 |
Correct |
0 ms |
3076 KB |
Output is correct |
4 |
Correct |
0 ms |
3076 KB |
Output is correct |
5 |
Correct |
0 ms |
3076 KB |
Output is correct |
6 |
Correct |
0 ms |
3076 KB |
Output is correct |
7 |
Correct |
0 ms |
3076 KB |
Output is correct |
8 |
Correct |
0 ms |
3076 KB |
Output is correct |
9 |
Correct |
0 ms |
3076 KB |
Output is correct |
10 |
Correct |
0 ms |
3076 KB |
Output is correct |
11 |
Correct |
0 ms |
3076 KB |
Output is correct |
12 |
Correct |
0 ms |
3076 KB |
Output is correct |
13 |
Correct |
0 ms |
3076 KB |
Output is correct |
14 |
Correct |
0 ms |
3076 KB |
Output is correct |
15 |
Correct |
0 ms |
3076 KB |
Output is correct |
16 |
Correct |
0 ms |
3076 KB |
Output is correct |
17 |
Correct |
0 ms |
3208 KB |
Output is correct |
18 |
Correct |
0 ms |
3076 KB |
Output is correct |
19 |
Correct |
0 ms |
3076 KB |
Output is correct |
20 |
Correct |
3 ms |
3208 KB |
Output is correct |
21 |
Correct |
0 ms |
3076 KB |
Output is correct |
22 |
Correct |
0 ms |
3076 KB |
Output is correct |
23 |
Correct |
0 ms |
3076 KB |
Output is correct |
24 |
Correct |
0 ms |
3220 KB |
Output is correct |
25 |
Correct |
0 ms |
3208 KB |
Output is correct |
26 |
Correct |
0 ms |
3076 KB |
Output is correct |
27 |
Correct |
0 ms |
3076 KB |
Output is correct |
28 |
Correct |
0 ms |
3208 KB |
Output is correct |
29 |
Correct |
3 ms |
3356 KB |
Output is correct |
30 |
Correct |
0 ms |
3220 KB |
Output is correct |
31 |
Correct |
0 ms |
3220 KB |
Output is correct |
32 |
Correct |
0 ms |
3224 KB |
Output is correct |
33 |
Correct |
3 ms |
3560 KB |
Output is correct |
34 |
Correct |
3 ms |
3560 KB |
Output is correct |
35 |
Correct |
3 ms |
3340 KB |
Output is correct |
36 |
Correct |
0 ms |
3208 KB |
Output is correct |
37 |
Correct |
6 ms |
3344 KB |
Output is correct |
38 |
Correct |
9 ms |
3524 KB |
Output is correct |
39 |
Correct |
9 ms |
3520 KB |
Output is correct |
40 |
Correct |
6 ms |
3524 KB |
Output is correct |
41 |
Correct |
9 ms |
3524 KB |
Output is correct |
42 |
Correct |
6 ms |
3352 KB |
Output is correct |
43 |
Correct |
6 ms |
3352 KB |
Output is correct |
44 |
Correct |
6 ms |
3348 KB |
Output is correct |
45 |
Correct |
16 ms |
3720 KB |
Output is correct |
46 |
Correct |
6 ms |
3724 KB |
Output is correct |
47 |
Correct |
16 ms |
4272 KB |
Output is correct |
48 |
Correct |
6 ms |
3604 KB |
Output is correct |
49 |
Correct |
6 ms |
3604 KB |
Output is correct |
50 |
Correct |
3 ms |
3472 KB |
Output is correct |
51 |
Correct |
33 ms |
4520 KB |
Output is correct |
52 |
Correct |
29 ms |
4524 KB |
Output is correct |
53 |
Correct |
16 ms |
4524 KB |
Output is correct |
54 |
Correct |
0 ms |
3348 KB |
Output is correct |
55 |
Correct |
0 ms |
3348 KB |
Output is correct |
56 |
Correct |
463 ms |
4000 KB |
Output is correct |
57 |
Correct |
49 ms |
4764 KB |
Output is correct |
58 |
Correct |
9 ms |
3416 KB |
Output is correct |
59 |
Correct |
6 ms |
3416 KB |
Output is correct |
60 |
Correct |
9 ms |
3424 KB |
Output is correct |
61 |
Correct |
3 ms |
3432 KB |
Output is correct |
62 |
Correct |
13 ms |
4000 KB |
Output is correct |
63 |
Correct |
53 ms |
6464 KB |
Output is correct |
64 |
Correct |
46 ms |
6428 KB |
Output is correct |
65 |
Correct |
56 ms |
6452 KB |
Output is correct |
66 |
Correct |
106 ms |
9480 KB |
Output is correct |
67 |
Correct |
116 ms |
9480 KB |
Output is correct |