#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;
//#define int long long
int n;int m;
int dis[30100];
vector<int>e[30100];
set<int>st[30100];
int vis[30100];
int ar[30100];
int br[30100];
int frm[30100];
signed main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>ar[i]>>br[i];
st[ar[i]].insert(br[i]);
// e[ar[i]].push_back(br[i]);
}
for(int i=0;i<n;i++){
for(auto it=st[i].begin();it!=st[i].end();it++){
e[i].push_back(*it);
}
}
for(int i=0;i<n;i++){
dis[i]=2000000000;
}
dis[ar[0]]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
pq.push({0,ar[0]});
while(pq.size()){
int nw=pq.top().second;
pq.pop();
if(vis[nw]){continue;}
vis[nw]=1;
// lft.erase(nw);
if(nw==ar[1]){
cout<<dis[nw]<<"\n";return 0;
}
for(int i=0;i<e[nw].size();i++){
int d=e[nw][i];
if(d==frm[nw]){continue;}
int v=nw+d;int t=1;
for(;v<n;){
//int v=nw+t*d;
// dis[v]=min(dis[v],dis[nw]+t);
if(dis[v]>dis[nw]+t){
dis[v]=dis[nw]+t;
frm[v]=d;
pq.push({dis[v],v});
}
v+=d;t++;
}
v=nw-d;t=1;
for(;v>=0;){
if(dis[v]>dis[nw]+t){
dis[v]=dis[nw]+t;
frm[v]=d;
pq.push({dis[v],v});
}
v-=d;t++;
}
}
}
if(dis[ar[1]]>1000000000){
cout<<-1<<endl;
return 0;
}
cout<<dis[ar[1]]<<endl;
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2676 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
3160 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2904 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2904 KB |
Output is correct |
33 |
Correct |
4 ms |
3296 KB |
Output is correct |
34 |
Correct |
2 ms |
3296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2848 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2956 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2940 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2904 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3296 KB |
Output is correct |
34 |
Correct |
2 ms |
3296 KB |
Output is correct |
35 |
Correct |
8 ms |
4208 KB |
Output is correct |
36 |
Correct |
2 ms |
3164 KB |
Output is correct |
37 |
Correct |
6 ms |
3972 KB |
Output is correct |
38 |
Correct |
9 ms |
4668 KB |
Output is correct |
39 |
Correct |
8 ms |
4568 KB |
Output is correct |
40 |
Correct |
8 ms |
4444 KB |
Output is correct |
41 |
Correct |
8 ms |
4524 KB |
Output is correct |
42 |
Correct |
4 ms |
3160 KB |
Output is correct |
43 |
Correct |
4 ms |
2960 KB |
Output is correct |
44 |
Correct |
4 ms |
2908 KB |
Output is correct |
45 |
Correct |
12 ms |
4700 KB |
Output is correct |
46 |
Correct |
11 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2848 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
1 ms |
2980 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
3164 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
3 ms |
3296 KB |
Output is correct |
34 |
Correct |
2 ms |
3400 KB |
Output is correct |
35 |
Correct |
11 ms |
4340 KB |
Output is correct |
36 |
Correct |
2 ms |
3164 KB |
Output is correct |
37 |
Correct |
6 ms |
3932 KB |
Output is correct |
38 |
Correct |
8 ms |
4444 KB |
Output is correct |
39 |
Correct |
8 ms |
4488 KB |
Output is correct |
40 |
Correct |
8 ms |
4444 KB |
Output is correct |
41 |
Correct |
8 ms |
4532 KB |
Output is correct |
42 |
Correct |
4 ms |
2904 KB |
Output is correct |
43 |
Correct |
4 ms |
2908 KB |
Output is correct |
44 |
Correct |
4 ms |
2908 KB |
Output is correct |
45 |
Correct |
11 ms |
4792 KB |
Output is correct |
46 |
Correct |
11 ms |
4828 KB |
Output is correct |
47 |
Correct |
8 ms |
5080 KB |
Output is correct |
48 |
Correct |
6 ms |
4440 KB |
Output is correct |
49 |
Correct |
6 ms |
4444 KB |
Output is correct |
50 |
Correct |
4 ms |
3932 KB |
Output is correct |
51 |
Correct |
13 ms |
6104 KB |
Output is correct |
52 |
Correct |
13 ms |
6008 KB |
Output is correct |
53 |
Correct |
9 ms |
5720 KB |
Output is correct |
54 |
Correct |
2 ms |
3164 KB |
Output is correct |
55 |
Correct |
2 ms |
3164 KB |
Output is correct |
56 |
Correct |
9 ms |
6052 KB |
Output is correct |
57 |
Correct |
2 ms |
3800 KB |
Output is correct |
58 |
Correct |
5 ms |
3316 KB |
Output is correct |
59 |
Correct |
6 ms |
3416 KB |
Output is correct |
60 |
Correct |
5 ms |
3420 KB |
Output is correct |
61 |
Correct |
6 ms |
3676 KB |
Output is correct |
62 |
Correct |
8 ms |
5724 KB |
Output is correct |
63 |
Correct |
29 ms |
7104 KB |
Output is correct |
64 |
Correct |
34 ms |
7380 KB |
Output is correct |
65 |
Correct |
36 ms |
7128 KB |
Output is correct |
66 |
Correct |
72 ms |
9684 KB |
Output is correct |
67 |
Correct |
42 ms |
9936 KB |
Output is correct |