#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >v[300005];
int sto[5005][5005];
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > >pq;
main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N,M;
cin>>N>>M;
for(int i=0;i<=N;i++)
for(int j=0;j<=N;j++)
sto[i][j] = 1e9;
bool occ[N+3];
memset(occ,0,sizeof(occ));
vector<pair<int,int> >hi;
for(int i=0;i<M;i++){
int a,b;
cin>>a>>b;
occ[a] = 1;
hi.push_back(make_pair(a,b));
}
for(int i=0;i<M;i++){
int x = 1;
while(hi[i].first-x*hi[i].second>=0){
//if(occ[hi[i].first-x*hi[i].second])v[hi[i].first].push_back(make_pair(hi[i].first-x*hi[i].second,x));
if(occ[hi[i].first-x*hi[i].second])sto[hi[i].first][hi[i].first-x*hi[i].second] = min(sto[hi[i].first][hi[i].first-x*hi[i].second],x);
x++;
}
x = 1;
while(hi[i].first+x*hi[i].second<N){
//if(occ[hi[i].first+x*hi[i].second])v[hi[i].first].push_back(make_pair(hi[i].first+x*hi[i].second,x));
if(occ[hi[i].first+x*hi[i].second])sto[hi[i].first][hi[i].first+x*hi[i].second] = min(sto[hi[i].first][hi[i].first+x*hi[i].second],x);
x++;
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(sto[i][j]!=1e9) v[i].push_back(make_pair(j,sto[i][j]));
}
}
pq.push(make_pair(0,hi[0].first));
int dist[300005];
for(int i=0;i<300004;i++) dist[i] = 1e9;
dist[hi[0].first] = 0;
while(!pq.empty()){
int dis = pq.top().first;
int no = pq.top().second;
pq.pop();
if(dist[no]<dis)continue;
for(pair<int,int>p:v[no]){
if(dis+p.second<dist[p.first]){
dist[p.first] = dis+p.second;
pq.push(make_pair(dis+p.second,p.first));
}
}
}
if(dist[hi[1].first] == 1e9) cout<<"-1";
else cout<<dist[hi[1].first];
}
Compilation message
skyscraper.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
6 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8556 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8660 KB |
Output is correct |
2 |
Correct |
5 ms |
8548 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
5 ms |
8532 KB |
Output is correct |
6 |
Correct |
5 ms |
8544 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8660 KB |
Output is correct |
9 |
Correct |
4 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
6 ms |
9044 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8612 KB |
Output is correct |
2 |
Correct |
4 ms |
8544 KB |
Output is correct |
3 |
Correct |
4 ms |
8480 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
5 ms |
8532 KB |
Output is correct |
8 |
Correct |
4 ms |
8672 KB |
Output is correct |
9 |
Correct |
5 ms |
8788 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
9044 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9032 KB |
Output is correct |
14 |
Correct |
5 ms |
9036 KB |
Output is correct |
15 |
Correct |
5 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
9556 KB |
Output is correct |
17 |
Correct |
8 ms |
13908 KB |
Output is correct |
18 |
Correct |
17 ms |
27476 KB |
Output is correct |
19 |
Correct |
19 ms |
32228 KB |
Output is correct |
20 |
Correct |
90 ms |
64404 KB |
Output is correct |
21 |
Correct |
6 ms |
10708 KB |
Output is correct |
22 |
Correct |
17 ms |
28116 KB |
Output is correct |
23 |
Correct |
16 ms |
25044 KB |
Output is correct |
24 |
Correct |
19 ms |
30488 KB |
Output is correct |
25 |
Correct |
20 ms |
32340 KB |
Output is correct |
26 |
Correct |
24 ms |
32236 KB |
Output is correct |
27 |
Correct |
24 ms |
32212 KB |
Output is correct |
28 |
Correct |
23 ms |
32412 KB |
Output is correct |
29 |
Correct |
20 ms |
32212 KB |
Output is correct |
30 |
Correct |
20 ms |
32252 KB |
Output is correct |
31 |
Correct |
20 ms |
32232 KB |
Output is correct |
32 |
Correct |
23 ms |
32212 KB |
Output is correct |
33 |
Correct |
21 ms |
32212 KB |
Output is correct |
34 |
Correct |
20 ms |
32300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8552 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8532 KB |
Output is correct |
6 |
Correct |
5 ms |
8548 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
5 ms |
8788 KB |
Output is correct |
9 |
Correct |
7 ms |
8780 KB |
Output is correct |
10 |
Correct |
5 ms |
8932 KB |
Output is correct |
11 |
Correct |
5 ms |
9044 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
8932 KB |
Output is correct |
15 |
Correct |
5 ms |
8936 KB |
Output is correct |
16 |
Correct |
6 ms |
9556 KB |
Output is correct |
17 |
Correct |
8 ms |
13936 KB |
Output is correct |
18 |
Correct |
17 ms |
27556 KB |
Output is correct |
19 |
Correct |
20 ms |
32232 KB |
Output is correct |
20 |
Correct |
91 ms |
64296 KB |
Output is correct |
21 |
Correct |
6 ms |
10708 KB |
Output is correct |
22 |
Correct |
18 ms |
28116 KB |
Output is correct |
23 |
Correct |
15 ms |
25028 KB |
Output is correct |
24 |
Correct |
20 ms |
30448 KB |
Output is correct |
25 |
Correct |
21 ms |
32376 KB |
Output is correct |
26 |
Correct |
26 ms |
32212 KB |
Output is correct |
27 |
Correct |
24 ms |
32212 KB |
Output is correct |
28 |
Correct |
21 ms |
32500 KB |
Output is correct |
29 |
Correct |
26 ms |
32176 KB |
Output is correct |
30 |
Correct |
20 ms |
32256 KB |
Output is correct |
31 |
Correct |
20 ms |
32236 KB |
Output is correct |
32 |
Correct |
21 ms |
32212 KB |
Output is correct |
33 |
Correct |
21 ms |
32264 KB |
Output is correct |
34 |
Correct |
20 ms |
32304 KB |
Output is correct |
35 |
Correct |
21 ms |
24084 KB |
Output is correct |
36 |
Correct |
12 ms |
17876 KB |
Output is correct |
37 |
Correct |
30 ms |
34184 KB |
Output is correct |
38 |
Correct |
31 ms |
34812 KB |
Output is correct |
39 |
Correct |
31 ms |
35056 KB |
Output is correct |
40 |
Correct |
36 ms |
34920 KB |
Output is correct |
41 |
Correct |
31 ms |
34880 KB |
Output is correct |
42 |
Correct |
102 ms |
32644 KB |
Output is correct |
43 |
Correct |
95 ms |
32800 KB |
Output is correct |
44 |
Correct |
363 ms |
64800 KB |
Output is correct |
45 |
Correct |
26 ms |
32768 KB |
Output is correct |
46 |
Correct |
25 ms |
32720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8532 KB |
Output is correct |
2 |
Correct |
4 ms |
8532 KB |
Output is correct |
3 |
Correct |
4 ms |
8532 KB |
Output is correct |
4 |
Correct |
4 ms |
8532 KB |
Output is correct |
5 |
Correct |
4 ms |
8548 KB |
Output is correct |
6 |
Correct |
4 ms |
8532 KB |
Output is correct |
7 |
Correct |
4 ms |
8532 KB |
Output is correct |
8 |
Correct |
4 ms |
8660 KB |
Output is correct |
9 |
Correct |
5 ms |
8796 KB |
Output is correct |
10 |
Correct |
4 ms |
8936 KB |
Output is correct |
11 |
Correct |
5 ms |
9044 KB |
Output is correct |
12 |
Correct |
6 ms |
8916 KB |
Output is correct |
13 |
Correct |
6 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
5 ms |
8968 KB |
Output is correct |
16 |
Correct |
5 ms |
9556 KB |
Output is correct |
17 |
Correct |
8 ms |
13908 KB |
Output is correct |
18 |
Correct |
17 ms |
27500 KB |
Output is correct |
19 |
Correct |
20 ms |
32212 KB |
Output is correct |
20 |
Correct |
89 ms |
64356 KB |
Output is correct |
21 |
Correct |
6 ms |
10728 KB |
Output is correct |
22 |
Correct |
18 ms |
28156 KB |
Output is correct |
23 |
Correct |
16 ms |
25044 KB |
Output is correct |
24 |
Correct |
19 ms |
30440 KB |
Output is correct |
25 |
Correct |
23 ms |
32388 KB |
Output is correct |
26 |
Correct |
26 ms |
32284 KB |
Output is correct |
27 |
Correct |
24 ms |
32244 KB |
Output is correct |
28 |
Correct |
21 ms |
32404 KB |
Output is correct |
29 |
Correct |
20 ms |
32228 KB |
Output is correct |
30 |
Correct |
20 ms |
32148 KB |
Output is correct |
31 |
Correct |
20 ms |
32212 KB |
Output is correct |
32 |
Correct |
20 ms |
32212 KB |
Output is correct |
33 |
Correct |
20 ms |
32300 KB |
Output is correct |
34 |
Correct |
20 ms |
32192 KB |
Output is correct |
35 |
Correct |
21 ms |
24168 KB |
Output is correct |
36 |
Correct |
11 ms |
17828 KB |
Output is correct |
37 |
Correct |
29 ms |
34296 KB |
Output is correct |
38 |
Correct |
31 ms |
34904 KB |
Output is correct |
39 |
Correct |
34 ms |
35108 KB |
Output is correct |
40 |
Correct |
30 ms |
34956 KB |
Output is correct |
41 |
Correct |
32 ms |
34768 KB |
Output is correct |
42 |
Correct |
106 ms |
32744 KB |
Output is correct |
43 |
Correct |
97 ms |
32804 KB |
Output is correct |
44 |
Correct |
347 ms |
64720 KB |
Output is correct |
45 |
Correct |
25 ms |
32752 KB |
Output is correct |
46 |
Correct |
25 ms |
32728 KB |
Output is correct |
47 |
Runtime error |
141 ms |
215860 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |