#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second
int n,m;
int pos[30000];
int gap[30000];
vector<int>gaps[30000];
int dist[30000];
int distv[30000];
void solve(){
for(int i=0;i<30000;i++){
dist[i]=1000000;
distv[i]=1000000;
}
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>pos[i]>>gap[i];
gaps[pos[i]].push_back(gap[i]);
}
dist[pos[0]]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
q.push({0,pos[0]});
while(!q.empty()){
auto [d,pos]=q.top();
q.pop();
if(d!=dist[pos]||distv[pos]==dist[pos])
continue;
distv[pos]=dist[pos];
for(int g:gaps[pos]){
for(int i=1;pos+g*i<n;i++){
if(dist[pos+g*i]>dist[pos]+i){
dist[pos+g*i]=dist[pos]+i;
q.push({dist[pos+g*i],pos+g*i});
}
}
for(int i=1;pos-g*i>=0;i++){
if(dist[pos-g*i]>dist[pos]+i){
dist[pos-g*i]=dist[pos]+i;
q.push({dist[pos-g*i],pos-g*i});
}
}
}
}
int res=dist[pos[1]];
if(res==1000000)
res=-1;
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
Compilation message
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:48:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
48 | auto [d,pos]=q.top();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1368 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1376 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1624 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1368 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1368 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
4 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
1624 KB |
Output is correct |
26 |
Correct |
3 ms |
1444 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1628 KB |
Output is correct |
34 |
Correct |
2 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1556 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1396 KB |
Output is correct |
16 |
Correct |
1 ms |
1624 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
1 ms |
1368 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1428 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1368 KB |
Output is correct |
25 |
Correct |
1 ms |
1368 KB |
Output is correct |
26 |
Correct |
3 ms |
1368 KB |
Output is correct |
27 |
Correct |
3 ms |
1368 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1628 KB |
Output is correct |
34 |
Correct |
2 ms |
1628 KB |
Output is correct |
35 |
Correct |
5 ms |
1884 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1628 KB |
Output is correct |
38 |
Correct |
6 ms |
1884 KB |
Output is correct |
39 |
Correct |
6 ms |
1796 KB |
Output is correct |
40 |
Correct |
6 ms |
1884 KB |
Output is correct |
41 |
Correct |
7 ms |
1760 KB |
Output is correct |
42 |
Correct |
38 ms |
1884 KB |
Output is correct |
43 |
Correct |
37 ms |
1628 KB |
Output is correct |
44 |
Correct |
36 ms |
1628 KB |
Output is correct |
45 |
Correct |
5 ms |
1884 KB |
Output is correct |
46 |
Correct |
6 ms |
2012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1372 KB |
Output is correct |
2 |
Correct |
1 ms |
1372 KB |
Output is correct |
3 |
Correct |
1 ms |
1372 KB |
Output is correct |
4 |
Correct |
1 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1372 KB |
Output is correct |
10 |
Correct |
1 ms |
1368 KB |
Output is correct |
11 |
Correct |
1 ms |
1372 KB |
Output is correct |
12 |
Correct |
1 ms |
1372 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1368 KB |
Output is correct |
19 |
Correct |
1 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1372 KB |
Output is correct |
23 |
Correct |
1 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
4 ms |
1368 KB |
Output is correct |
27 |
Correct |
3 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
2 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
2 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1464 KB |
Output is correct |
35 |
Correct |
5 ms |
1884 KB |
Output is correct |
36 |
Correct |
2 ms |
1372 KB |
Output is correct |
37 |
Correct |
4 ms |
1624 KB |
Output is correct |
38 |
Correct |
6 ms |
2136 KB |
Output is correct |
39 |
Correct |
6 ms |
1880 KB |
Output is correct |
40 |
Correct |
6 ms |
1884 KB |
Output is correct |
41 |
Correct |
6 ms |
2080 KB |
Output is correct |
42 |
Correct |
36 ms |
1628 KB |
Output is correct |
43 |
Correct |
38 ms |
1860 KB |
Output is correct |
44 |
Correct |
36 ms |
1628 KB |
Output is correct |
45 |
Correct |
5 ms |
1884 KB |
Output is correct |
46 |
Correct |
5 ms |
1952 KB |
Output is correct |
47 |
Correct |
12 ms |
2520 KB |
Output is correct |
48 |
Correct |
4 ms |
1884 KB |
Output is correct |
49 |
Correct |
4 ms |
1884 KB |
Output is correct |
50 |
Correct |
3 ms |
1624 KB |
Output is correct |
51 |
Correct |
19 ms |
3284 KB |
Output is correct |
52 |
Correct |
18 ms |
2776 KB |
Output is correct |
53 |
Correct |
10 ms |
2776 KB |
Output is correct |
54 |
Correct |
2 ms |
1628 KB |
Output is correct |
55 |
Correct |
2 ms |
1628 KB |
Output is correct |
56 |
Correct |
532 ms |
2908 KB |
Output is correct |
57 |
Correct |
37 ms |
3536 KB |
Output is correct |
58 |
Correct |
618 ms |
1884 KB |
Output is correct |
59 |
Correct |
485 ms |
2012 KB |
Output is correct |
60 |
Correct |
564 ms |
1880 KB |
Output is correct |
61 |
Correct |
444 ms |
2140 KB |
Output is correct |
62 |
Correct |
7 ms |
2652 KB |
Output is correct |
63 |
Correct |
23 ms |
3036 KB |
Output is correct |
64 |
Correct |
24 ms |
4048 KB |
Output is correct |
65 |
Correct |
28 ms |
4052 KB |
Output is correct |
66 |
Correct |
40 ms |
4220 KB |
Output is correct |
67 |
Correct |
40 ms |
4048 KB |
Output is correct |