/*
Code by @marlov
*/
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <utility>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <iterator>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
#define INF 200000000
#define maxV 30005
int N,M;
pair<int,int> doge[maxV];
//distance to location 0-N-1 inclusive
int dist[maxV];
bool vdoge[maxV];
priority_queue< pair<int,int> > pq;
//location contains array
vector<int> lc[maxV];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>M;
fill(dist,dist+N,INF);
for(int i=0;i<M;i++){
cin>>doge[i].first>>doge[i].second;
lc[doge[i].first].push_back(i);
}
for(int j=0;j<lc[doge[0].first].size();j++){
pq.push(make_pair(0,lc[doge[0].first][j]));
}
dist[doge[0].first]=0;
while(!pq.empty()){
//cd is number of jumps and ci is the current index
int cd=-1*pq.top().first;
int ci=pq.top().second;
pq.pop();
//if that dog has already been visited there is no need to check again
if(vdoge[ci]) continue;
vdoge[ci]=true;
//loops from current location to 0
for(int i=0, cl=doge[ci].first;cl>=0;i++, cl -= doge[ci].second){
//current location
if(cd+i<dist[cl]){
dist[cl]=cd+i;
for(int j=0;j<lc[cl].size();j++){
pq.push(make_pair(-1*dist[cl],lc[cl][j]));
}
}
}
//to N-1
for(int i=1, cl=doge[ci].first+doge[ci].second;cl<N;cl += doge[ci].second, i++){
if(cd+i<dist[cl]){
dist[cl]=cd+i;
for(int j=0;j<lc[cl].size();j++){
pq.push(make_pair(-1*dist[cl],lc[cl][j]));
}
}
}
}
if(dist[doge[1].first]==INF){
cout<<-1<<'\n';
}else{
cout<<dist[doge[1].first]<<'\n';
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1,n=0?)
* do smth instead of nothing and stay organized
*/
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<lc[doge[0].first].size();j++){
~^~~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:64:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<lc[cl].size();j++){
~^~~~~~~~~~~~~~
skyscraper.cpp:73:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<lc[cl].size();j++){
~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1152 KB |
Output is correct |
12 |
Correct |
2 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
1152 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
1 ms |
1152 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
1152 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1024 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
1152 KB |
Output is correct |
24 |
Correct |
2 ms |
1152 KB |
Output is correct |
25 |
Correct |
2 ms |
1152 KB |
Output is correct |
26 |
Correct |
4 ms |
1152 KB |
Output is correct |
27 |
Correct |
4 ms |
1152 KB |
Output is correct |
28 |
Correct |
1 ms |
1152 KB |
Output is correct |
29 |
Correct |
1 ms |
1024 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
1024 KB |
Output is correct |
32 |
Correct |
1 ms |
1024 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
2 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1152 KB |
Output is correct |
12 |
Correct |
2 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
1152 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
1 ms |
1152 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1024 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
1152 KB |
Output is correct |
24 |
Correct |
2 ms |
1152 KB |
Output is correct |
25 |
Correct |
2 ms |
1152 KB |
Output is correct |
26 |
Correct |
4 ms |
1152 KB |
Output is correct |
27 |
Correct |
4 ms |
1152 KB |
Output is correct |
28 |
Correct |
2 ms |
1152 KB |
Output is correct |
29 |
Correct |
1 ms |
1024 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
1024 KB |
Output is correct |
32 |
Correct |
1 ms |
1024 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
2 ms |
1152 KB |
Output is correct |
35 |
Correct |
19 ms |
2172 KB |
Output is correct |
36 |
Correct |
4 ms |
1280 KB |
Output is correct |
37 |
Correct |
15 ms |
2044 KB |
Output is correct |
38 |
Correct |
24 ms |
2680 KB |
Output is correct |
39 |
Correct |
21 ms |
2680 KB |
Output is correct |
40 |
Correct |
28 ms |
2680 KB |
Output is correct |
41 |
Correct |
24 ms |
2680 KB |
Output is correct |
42 |
Correct |
56 ms |
1912 KB |
Output is correct |
43 |
Correct |
53 ms |
1792 KB |
Output is correct |
44 |
Correct |
65 ms |
1856 KB |
Output is correct |
45 |
Correct |
18 ms |
2172 KB |
Output is correct |
46 |
Correct |
17 ms |
2172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
1024 KB |
Output is correct |
3 |
Correct |
1 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1024 KB |
Output is correct |
7 |
Correct |
1 ms |
1024 KB |
Output is correct |
8 |
Correct |
1 ms |
1024 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 KB |
Output is correct |
11 |
Correct |
2 ms |
1152 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
1152 KB |
Output is correct |
14 |
Correct |
1 ms |
1152 KB |
Output is correct |
15 |
Correct |
1 ms |
1152 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
2 ms |
1152 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
1152 KB |
Output is correct |
21 |
Correct |
1 ms |
1024 KB |
Output is correct |
22 |
Correct |
1 ms |
1024 KB |
Output is correct |
23 |
Correct |
1 ms |
1152 KB |
Output is correct |
24 |
Correct |
2 ms |
1152 KB |
Output is correct |
25 |
Correct |
2 ms |
1152 KB |
Output is correct |
26 |
Correct |
4 ms |
1152 KB |
Output is correct |
27 |
Correct |
4 ms |
1152 KB |
Output is correct |
28 |
Correct |
1 ms |
1152 KB |
Output is correct |
29 |
Correct |
1 ms |
1152 KB |
Output is correct |
30 |
Correct |
1 ms |
1024 KB |
Output is correct |
31 |
Correct |
1 ms |
1024 KB |
Output is correct |
32 |
Correct |
1 ms |
1024 KB |
Output is correct |
33 |
Correct |
2 ms |
1152 KB |
Output is correct |
34 |
Correct |
2 ms |
1152 KB |
Output is correct |
35 |
Correct |
19 ms |
2172 KB |
Output is correct |
36 |
Correct |
3 ms |
1280 KB |
Output is correct |
37 |
Correct |
13 ms |
2044 KB |
Output is correct |
38 |
Correct |
23 ms |
2680 KB |
Output is correct |
39 |
Correct |
22 ms |
2680 KB |
Output is correct |
40 |
Correct |
23 ms |
2680 KB |
Output is correct |
41 |
Correct |
23 ms |
2680 KB |
Output is correct |
42 |
Correct |
50 ms |
1920 KB |
Output is correct |
43 |
Correct |
51 ms |
1792 KB |
Output is correct |
44 |
Correct |
51 ms |
1888 KB |
Output is correct |
45 |
Correct |
17 ms |
2172 KB |
Output is correct |
46 |
Correct |
16 ms |
2172 KB |
Output is correct |
47 |
Correct |
28 ms |
2808 KB |
Output is correct |
48 |
Correct |
7 ms |
1792 KB |
Output is correct |
49 |
Correct |
7 ms |
1792 KB |
Output is correct |
50 |
Correct |
7 ms |
1664 KB |
Output is correct |
51 |
Correct |
29 ms |
3184 KB |
Output is correct |
52 |
Correct |
28 ms |
2684 KB |
Output is correct |
53 |
Correct |
17 ms |
2684 KB |
Output is correct |
54 |
Correct |
1 ms |
1152 KB |
Output is correct |
55 |
Correct |
1 ms |
1152 KB |
Output is correct |
56 |
Correct |
683 ms |
2688 KB |
Output is correct |
57 |
Correct |
11 ms |
1532 KB |
Output is correct |
58 |
Correct |
715 ms |
1940 KB |
Output is correct |
59 |
Correct |
611 ms |
2164 KB |
Output is correct |
60 |
Correct |
697 ms |
2172 KB |
Output is correct |
61 |
Correct |
564 ms |
2292 KB |
Output is correct |
62 |
Correct |
11 ms |
2304 KB |
Output is correct |
63 |
Correct |
16 ms |
2048 KB |
Output is correct |
64 |
Correct |
16 ms |
2048 KB |
Output is correct |
65 |
Correct |
21 ms |
2048 KB |
Output is correct |
66 |
Correct |
26 ms |
2300 KB |
Output is correct |
67 |
Correct |
25 ms |
2300 KB |
Output is correct |