#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n,m;
int aa,bb;
const int ss=7;
vector<pair<pair<int,int>,int>> adj[30001][ss];//((index,power),dist)
int dist[30001][ss];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
int st;
int endd;
memset(dist,-1,sizeof(dist));
for(int j=0;j<m;j++){
cin>>aa>>bb;
if(j==0){
st=aa;
}
if(j==1){
endd=aa;
}
if(bb>=ss){
for(int i=aa+bb;i<n;i+=bb){
adj[aa][0].pb({{i,0},(i-aa)/bb});
}
for(int i=aa-bb;i>=0;i-=bb){
adj[aa][0].pb({{i,0},(aa-i)/bb});
}
}
else{
adj[aa][0].pb({{aa,bb},0});
// adj[aa][bb].pb({{aa,0},0});
}
}
/*for(int i=0;i<n;i++){
for(int j=1;j<ss;j++){
if(i+j<n){
adj[i][j].pb({{i+j,j},1});
}
if(i-j>=0 and i-j<n){
adj[i][j].pb({{i-j,j},1});
}
adj[i][j].pb({{i,0},0});
}
}*/
priority_queue<pair<int,pair<int,int>>> ac;
ac.push({0,{st,0}});
dist[st][0]=0;
while(ac.size()){
pair<int,pair<int,int>> no=ac.top();
ac.pop();
no.a=-no.a;
if(dist[no.b.a][no.b.b]<no.a){
continue;
}
// cout<<no.b.a<<","<<no.b.b<<","<<no.a<<endl;
for(auto j:adj[no.b.a][no.b.b]){
if(dist[j.a.a][j.a.b]==-1 or dist[j.a.a][j.a.b]>dist[no.b.a][no.b.b]+j.b){
dist[j.a.a][j.a.b]=dist[no.b.a][no.b.b]+j.b;
ac.push({-dist[j.a.a][j.a.b],{j.a}});
}
}
if(no.b.b>0){
if(no.b.a+no.b.b<n){
if(dist[no.b.a+no.b.b][no.b.b]==-1 or dist[no.b.a+no.b.b][no.b.b]>dist[no.b.a][no.b.b]+1){
dist[no.b.a+no.b.b][no.b.b]=dist[no.b.a][no.b.b]+1;
ac.push({-dist[no.b.a+no.b.b][no.b.b],{no.b.a+no.b.b,no.b.b}});
}
}
if(no.b.a-no.b.b>=0){
if(dist[no.b.a-no.b.b][no.b.b]==-1 or dist[no.b.a-no.b.b][no.b.b]>dist[no.b.a][no.b.b]+1){
dist[no.b.a-no.b.b][no.b.b]=dist[no.b.a][no.b.b]+1;
ac.push({-dist[no.b.a-no.b.b][no.b.b],{no.b.a-no.b.b,no.b.b}});
}
}
if(dist[no.b.a][0]==-1 or dist[no.b.a][0]>dist[no.b.a][no.b.b]){
dist[no.b.a][0]=dist[no.b.a][no.b.b];
ac.push({-dist[no.b.a][0],{no.b.a,0}});
}
}
}
cout<<dist[endd][0]<<endl;
return 0;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:56:13: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
dist[st][0]=0;
~~~~~~~~~~~^~
skyscraper.cpp:92:20: warning: 'endd' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<dist[endd][0]<<endl;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6016 KB |
Output is correct |
2 |
Correct |
8 ms |
6016 KB |
Output is correct |
3 |
Correct |
7 ms |
6144 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
10 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
6016 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
6144 KB |
Output is correct |
2 |
Correct |
7 ms |
6016 KB |
Output is correct |
3 |
Correct |
8 ms |
6016 KB |
Output is correct |
4 |
Correct |
8 ms |
6016 KB |
Output is correct |
5 |
Correct |
8 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
6144 KB |
Output is correct |
7 |
Correct |
8 ms |
6016 KB |
Output is correct |
8 |
Correct |
8 ms |
6016 KB |
Output is correct |
9 |
Correct |
8 ms |
6016 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
11 |
Correct |
9 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6016 KB |
Output is correct |
2 |
Correct |
7 ms |
6016 KB |
Output is correct |
3 |
Correct |
8 ms |
6144 KB |
Output is correct |
4 |
Correct |
8 ms |
6016 KB |
Output is correct |
5 |
Correct |
9 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
6016 KB |
Output is correct |
7 |
Correct |
8 ms |
6016 KB |
Output is correct |
8 |
Correct |
8 ms |
6016 KB |
Output is correct |
9 |
Correct |
8 ms |
6016 KB |
Output is correct |
10 |
Correct |
9 ms |
6144 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
9 ms |
6144 KB |
Output is correct |
16 |
Correct |
8 ms |
6144 KB |
Output is correct |
17 |
Correct |
10 ms |
6272 KB |
Output is correct |
18 |
Correct |
8 ms |
6144 KB |
Output is correct |
19 |
Correct |
8 ms |
6144 KB |
Output is correct |
20 |
Correct |
8 ms |
6144 KB |
Output is correct |
21 |
Correct |
8 ms |
6144 KB |
Output is correct |
22 |
Correct |
8 ms |
6144 KB |
Output is correct |
23 |
Correct |
9 ms |
6144 KB |
Output is correct |
24 |
Correct |
9 ms |
6272 KB |
Output is correct |
25 |
Correct |
8 ms |
6144 KB |
Output is correct |
26 |
Correct |
8 ms |
6272 KB |
Output is correct |
27 |
Correct |
9 ms |
6272 KB |
Output is correct |
28 |
Correct |
9 ms |
6272 KB |
Output is correct |
29 |
Correct |
10 ms |
7168 KB |
Output is correct |
30 |
Correct |
9 ms |
6400 KB |
Output is correct |
31 |
Correct |
10 ms |
6656 KB |
Output is correct |
32 |
Correct |
9 ms |
6400 KB |
Output is correct |
33 |
Correct |
12 ms |
8064 KB |
Output is correct |
34 |
Correct |
13 ms |
8064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6016 KB |
Output is correct |
2 |
Correct |
8 ms |
6144 KB |
Output is correct |
3 |
Correct |
8 ms |
6016 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
8 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
6016 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6144 KB |
Output is correct |
9 |
Correct |
8 ms |
6144 KB |
Output is correct |
10 |
Correct |
8 ms |
6144 KB |
Output is correct |
11 |
Correct |
9 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
10 ms |
6144 KB |
Output is correct |
17 |
Correct |
9 ms |
6400 KB |
Output is correct |
18 |
Correct |
8 ms |
6144 KB |
Output is correct |
19 |
Correct |
8 ms |
6144 KB |
Output is correct |
20 |
Correct |
9 ms |
6144 KB |
Output is correct |
21 |
Correct |
8 ms |
6144 KB |
Output is correct |
22 |
Correct |
8 ms |
6144 KB |
Output is correct |
23 |
Correct |
9 ms |
6144 KB |
Output is correct |
24 |
Correct |
10 ms |
6272 KB |
Output is correct |
25 |
Correct |
9 ms |
6144 KB |
Output is correct |
26 |
Correct |
9 ms |
6272 KB |
Output is correct |
27 |
Correct |
9 ms |
6144 KB |
Output is correct |
28 |
Correct |
9 ms |
6272 KB |
Output is correct |
29 |
Correct |
10 ms |
7168 KB |
Output is correct |
30 |
Correct |
9 ms |
6400 KB |
Output is correct |
31 |
Correct |
10 ms |
6656 KB |
Output is correct |
32 |
Correct |
9 ms |
6400 KB |
Output is correct |
33 |
Correct |
13 ms |
8064 KB |
Output is correct |
34 |
Correct |
13 ms |
8064 KB |
Output is correct |
35 |
Correct |
18 ms |
7680 KB |
Output is correct |
36 |
Correct |
10 ms |
6272 KB |
Output is correct |
37 |
Correct |
19 ms |
9600 KB |
Output is correct |
38 |
Correct |
22 ms |
8960 KB |
Output is correct |
39 |
Correct |
21 ms |
8832 KB |
Output is correct |
40 |
Correct |
22 ms |
8832 KB |
Output is correct |
41 |
Correct |
21 ms |
8704 KB |
Output is correct |
42 |
Correct |
14 ms |
6784 KB |
Output is correct |
43 |
Correct |
13 ms |
6652 KB |
Output is correct |
44 |
Correct |
13 ms |
6612 KB |
Output is correct |
45 |
Correct |
24 ms |
13056 KB |
Output is correct |
46 |
Correct |
23 ms |
13056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
6016 KB |
Output is correct |
2 |
Correct |
8 ms |
6016 KB |
Output is correct |
3 |
Correct |
8 ms |
6016 KB |
Output is correct |
4 |
Correct |
8 ms |
6144 KB |
Output is correct |
5 |
Correct |
9 ms |
6016 KB |
Output is correct |
6 |
Correct |
8 ms |
6016 KB |
Output is correct |
7 |
Correct |
8 ms |
6144 KB |
Output is correct |
8 |
Correct |
8 ms |
6016 KB |
Output is correct |
9 |
Correct |
8 ms |
6144 KB |
Output is correct |
10 |
Correct |
9 ms |
6144 KB |
Output is correct |
11 |
Correct |
8 ms |
6144 KB |
Output is correct |
12 |
Correct |
8 ms |
6144 KB |
Output is correct |
13 |
Correct |
8 ms |
6144 KB |
Output is correct |
14 |
Correct |
8 ms |
6144 KB |
Output is correct |
15 |
Correct |
8 ms |
6144 KB |
Output is correct |
16 |
Correct |
10 ms |
6144 KB |
Output is correct |
17 |
Correct |
9 ms |
6272 KB |
Output is correct |
18 |
Correct |
10 ms |
6144 KB |
Output is correct |
19 |
Correct |
8 ms |
6144 KB |
Output is correct |
20 |
Correct |
8 ms |
6144 KB |
Output is correct |
21 |
Correct |
8 ms |
6144 KB |
Output is correct |
22 |
Correct |
8 ms |
6144 KB |
Output is correct |
23 |
Correct |
9 ms |
6144 KB |
Output is correct |
24 |
Correct |
13 ms |
6272 KB |
Output is correct |
25 |
Correct |
9 ms |
6144 KB |
Output is correct |
26 |
Correct |
9 ms |
6272 KB |
Output is correct |
27 |
Correct |
10 ms |
6144 KB |
Output is correct |
28 |
Correct |
9 ms |
6272 KB |
Output is correct |
29 |
Correct |
10 ms |
7168 KB |
Output is correct |
30 |
Correct |
8 ms |
6400 KB |
Output is correct |
31 |
Correct |
11 ms |
6656 KB |
Output is correct |
32 |
Correct |
9 ms |
6400 KB |
Output is correct |
33 |
Correct |
15 ms |
8064 KB |
Output is correct |
34 |
Correct |
12 ms |
8064 KB |
Output is correct |
35 |
Correct |
17 ms |
7680 KB |
Output is correct |
36 |
Correct |
11 ms |
6272 KB |
Output is correct |
37 |
Correct |
21 ms |
9600 KB |
Output is correct |
38 |
Correct |
22 ms |
8960 KB |
Output is correct |
39 |
Correct |
22 ms |
8832 KB |
Output is correct |
40 |
Correct |
23 ms |
8832 KB |
Output is correct |
41 |
Correct |
21 ms |
8832 KB |
Output is correct |
42 |
Correct |
15 ms |
6724 KB |
Output is correct |
43 |
Correct |
14 ms |
6720 KB |
Output is correct |
44 |
Correct |
14 ms |
6612 KB |
Output is correct |
45 |
Correct |
25 ms |
13056 KB |
Output is correct |
46 |
Correct |
27 ms |
13072 KB |
Output is correct |
47 |
Correct |
63 ms |
19192 KB |
Output is correct |
48 |
Correct |
24 ms |
8936 KB |
Output is correct |
49 |
Correct |
18 ms |
8064 KB |
Output is correct |
50 |
Correct |
17 ms |
8064 KB |
Output is correct |
51 |
Correct |
57 ms |
10596 KB |
Output is correct |
52 |
Correct |
58 ms |
10868 KB |
Output is correct |
53 |
Correct |
23 ms |
6272 KB |
Output is correct |
54 |
Correct |
10 ms |
6144 KB |
Output is correct |
55 |
Correct |
11 ms |
6016 KB |
Output is correct |
56 |
Correct |
23 ms |
6984 KB |
Output is correct |
57 |
Correct |
150 ms |
55944 KB |
Output is correct |
58 |
Correct |
17 ms |
7036 KB |
Output is correct |
59 |
Correct |
21 ms |
7676 KB |
Output is correct |
60 |
Correct |
22 ms |
8564 KB |
Output is correct |
61 |
Correct |
21 ms |
7360 KB |
Output is correct |
62 |
Correct |
43 ms |
12264 KB |
Output is correct |
63 |
Correct |
80 ms |
41140 KB |
Output is correct |
64 |
Correct |
98 ms |
54324 KB |
Output is correct |
65 |
Correct |
129 ms |
63512 KB |
Output is correct |
66 |
Correct |
220 ms |
106472 KB |
Output is correct |
67 |
Correct |
217 ms |
109516 KB |
Output is correct |