//Never stop trying
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const int MOD = 1e9 + 7; //998244353
const ll INF = 2e9;
const int MX = 3e4 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
return a + rng() % (b - a + 1);
}
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl;
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/////////////////////////ONLY CLEAN CODES ALLOWED/////////////////////////
const int mxP=40;
int N,M;
vi pos(MX),vec[MX],P(MX);
V<pair<pi,int>> adj[MX][mxP+1];
int main() {
boost; IO();
cin>>N>>M;
FOR(i,0,M){
cin>>pos[i]>>P[i];
if(P[i]<=mxP) vec[pos[i]].pb(P[i]);
}
FOR(i,0,M) if(P[i]>mxP){
int x=pos[i],jump=0;
while(x+P[i]*(jump+1)<N){
jump++;
adj[x][0].pb({{x+P[i]*jump,0},jump});
}
jump=0;
while(x-P[i]*(jump+1)>=0){
jump++;
adj[x][0].pb({{x-P[i]*jump,0},jump});
}
}
priority_queue<pair<int,pi>,V<pair<int,pi>>,greater<pair<int,pi>>>q;
q.push({0,{pos[0],0}});
int dist[N][mxP+1]; FOR(i,0,N) FOR(j,0,mxP+1) dist[i][j]=INF;
dist[pos[0]][0]=0;
while(!q.empty()){
int u=q.top().se.fi,p=q.top().se.se,d=q.top().fi;
q.pop();
if(dist[u][p]<d) continue;
for(auto it: adj[u][p]){
int v=it.fi.fi,pp=it.fi.se,w=it.se;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
}
if(u+p<N){
int v=u+p,pp=p,w=1;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
pp=0;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
}
if(u-p>=0){
int v=u-p,pp=p,w=1;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
pp=0;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
}
for(auto pp: vec[u]){
int v=u,w=0;
if(dist[v][pp]>d+w){
dist[v][pp]=d+w;
q.push({dist[v][pp],{v,pp}});
}
}
}
int ans=dist[pos[1]][0];
if(ans==INF) ans=-1;
cout << ans << endl;
return 0;
}
/*
(pos,power)-->(pos+-power,power) (<=sqrt(N))
(pos,power)-->(pos+-power,0) (<=sqrt(N))
(pos,0)--> (pos,power) (all powers in that pos) (<=sqrt(N))
(pos,0)-->(nw_pos,0) (>sqrt(N))
*/
//Change your approach
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30060 KB |
Output is correct |
7 |
Correct |
23 ms |
30188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
22 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
19 ms |
30188 KB |
Output is correct |
9 |
Correct |
19 ms |
30188 KB |
Output is correct |
10 |
Correct |
20 ms |
30188 KB |
Output is correct |
11 |
Correct |
20 ms |
30188 KB |
Output is correct |
12 |
Correct |
19 ms |
30188 KB |
Output is correct |
13 |
Correct |
21 ms |
30188 KB |
Output is correct |
14 |
Correct |
22 ms |
30188 KB |
Output is correct |
15 |
Correct |
22 ms |
30188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
24 ms |
30184 KB |
Output is correct |
8 |
Correct |
23 ms |
30164 KB |
Output is correct |
9 |
Correct |
21 ms |
30188 KB |
Output is correct |
10 |
Correct |
21 ms |
30188 KB |
Output is correct |
11 |
Correct |
22 ms |
30188 KB |
Output is correct |
12 |
Correct |
24 ms |
30188 KB |
Output is correct |
13 |
Correct |
23 ms |
30188 KB |
Output is correct |
14 |
Correct |
22 ms |
30232 KB |
Output is correct |
15 |
Correct |
24 ms |
30188 KB |
Output is correct |
16 |
Correct |
23 ms |
30280 KB |
Output is correct |
17 |
Correct |
25 ms |
30444 KB |
Output is correct |
18 |
Correct |
22 ms |
30444 KB |
Output is correct |
19 |
Correct |
23 ms |
30572 KB |
Output is correct |
20 |
Correct |
22 ms |
30572 KB |
Output is correct |
21 |
Correct |
21 ms |
30188 KB |
Output is correct |
22 |
Correct |
21 ms |
30444 KB |
Output is correct |
23 |
Correct |
22 ms |
30444 KB |
Output is correct |
24 |
Correct |
24 ms |
30576 KB |
Output is correct |
25 |
Correct |
23 ms |
30572 KB |
Output is correct |
26 |
Correct |
22 ms |
30572 KB |
Output is correct |
27 |
Correct |
28 ms |
30720 KB |
Output is correct |
28 |
Correct |
27 ms |
30572 KB |
Output is correct |
29 |
Correct |
30 ms |
30572 KB |
Output is correct |
30 |
Correct |
24 ms |
30444 KB |
Output is correct |
31 |
Correct |
25 ms |
30828 KB |
Output is correct |
32 |
Correct |
26 ms |
30828 KB |
Output is correct |
33 |
Correct |
40 ms |
31340 KB |
Output is correct |
34 |
Correct |
34 ms |
31340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
30188 KB |
Output is correct |
2 |
Correct |
23 ms |
30188 KB |
Output is correct |
3 |
Correct |
21 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
25 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30188 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
21 ms |
30188 KB |
Output is correct |
9 |
Correct |
25 ms |
30188 KB |
Output is correct |
10 |
Correct |
23 ms |
30316 KB |
Output is correct |
11 |
Correct |
25 ms |
30188 KB |
Output is correct |
12 |
Correct |
24 ms |
30188 KB |
Output is correct |
13 |
Correct |
21 ms |
30188 KB |
Output is correct |
14 |
Correct |
22 ms |
30188 KB |
Output is correct |
15 |
Correct |
21 ms |
30188 KB |
Output is correct |
16 |
Correct |
21 ms |
30188 KB |
Output is correct |
17 |
Correct |
23 ms |
30444 KB |
Output is correct |
18 |
Correct |
21 ms |
30444 KB |
Output is correct |
19 |
Correct |
21 ms |
30572 KB |
Output is correct |
20 |
Correct |
22 ms |
30572 KB |
Output is correct |
21 |
Correct |
21 ms |
30188 KB |
Output is correct |
22 |
Correct |
22 ms |
30444 KB |
Output is correct |
23 |
Correct |
22 ms |
30444 KB |
Output is correct |
24 |
Correct |
23 ms |
30572 KB |
Output is correct |
25 |
Correct |
22 ms |
30572 KB |
Output is correct |
26 |
Correct |
22 ms |
30572 KB |
Output is correct |
27 |
Correct |
22 ms |
30572 KB |
Output is correct |
28 |
Correct |
23 ms |
30572 KB |
Output is correct |
29 |
Correct |
30 ms |
30572 KB |
Output is correct |
30 |
Correct |
24 ms |
30444 KB |
Output is correct |
31 |
Correct |
25 ms |
30700 KB |
Output is correct |
32 |
Correct |
23 ms |
30828 KB |
Output is correct |
33 |
Correct |
34 ms |
31340 KB |
Output is correct |
34 |
Correct |
34 ms |
31340 KB |
Output is correct |
35 |
Correct |
33 ms |
31340 KB |
Output is correct |
36 |
Correct |
23 ms |
30444 KB |
Output is correct |
37 |
Correct |
38 ms |
32620 KB |
Output is correct |
38 |
Correct |
38 ms |
32236 KB |
Output is correct |
39 |
Correct |
39 ms |
32236 KB |
Output is correct |
40 |
Correct |
39 ms |
32364 KB |
Output is correct |
41 |
Correct |
44 ms |
32384 KB |
Output is correct |
42 |
Correct |
28 ms |
30700 KB |
Output is correct |
43 |
Correct |
26 ms |
30700 KB |
Output is correct |
44 |
Correct |
26 ms |
30700 KB |
Output is correct |
45 |
Correct |
45 ms |
36460 KB |
Output is correct |
46 |
Correct |
45 ms |
36460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
30188 KB |
Output is correct |
2 |
Correct |
21 ms |
30188 KB |
Output is correct |
3 |
Correct |
23 ms |
30188 KB |
Output is correct |
4 |
Correct |
21 ms |
30188 KB |
Output is correct |
5 |
Correct |
21 ms |
30188 KB |
Output is correct |
6 |
Correct |
21 ms |
30316 KB |
Output is correct |
7 |
Correct |
21 ms |
30188 KB |
Output is correct |
8 |
Correct |
21 ms |
30224 KB |
Output is correct |
9 |
Correct |
21 ms |
30188 KB |
Output is correct |
10 |
Correct |
21 ms |
30188 KB |
Output is correct |
11 |
Correct |
22 ms |
30188 KB |
Output is correct |
12 |
Correct |
21 ms |
30188 KB |
Output is correct |
13 |
Correct |
21 ms |
30188 KB |
Output is correct |
14 |
Correct |
22 ms |
30188 KB |
Output is correct |
15 |
Correct |
21 ms |
30188 KB |
Output is correct |
16 |
Correct |
21 ms |
30188 KB |
Output is correct |
17 |
Correct |
23 ms |
30444 KB |
Output is correct |
18 |
Correct |
21 ms |
30444 KB |
Output is correct |
19 |
Correct |
22 ms |
30572 KB |
Output is correct |
20 |
Correct |
22 ms |
30572 KB |
Output is correct |
21 |
Correct |
21 ms |
30188 KB |
Output is correct |
22 |
Correct |
21 ms |
30464 KB |
Output is correct |
23 |
Correct |
22 ms |
30444 KB |
Output is correct |
24 |
Correct |
23 ms |
30572 KB |
Output is correct |
25 |
Correct |
22 ms |
30572 KB |
Output is correct |
26 |
Correct |
22 ms |
30572 KB |
Output is correct |
27 |
Correct |
25 ms |
30572 KB |
Output is correct |
28 |
Correct |
23 ms |
30700 KB |
Output is correct |
29 |
Correct |
31 ms |
30572 KB |
Output is correct |
30 |
Correct |
24 ms |
30444 KB |
Output is correct |
31 |
Correct |
27 ms |
30700 KB |
Output is correct |
32 |
Correct |
23 ms |
30828 KB |
Output is correct |
33 |
Correct |
34 ms |
31340 KB |
Output is correct |
34 |
Correct |
34 ms |
31340 KB |
Output is correct |
35 |
Correct |
33 ms |
31340 KB |
Output is correct |
36 |
Correct |
23 ms |
30444 KB |
Output is correct |
37 |
Correct |
38 ms |
32748 KB |
Output is correct |
38 |
Correct |
38 ms |
32236 KB |
Output is correct |
39 |
Correct |
39 ms |
32236 KB |
Output is correct |
40 |
Correct |
39 ms |
32364 KB |
Output is correct |
41 |
Correct |
39 ms |
32364 KB |
Output is correct |
42 |
Correct |
27 ms |
30700 KB |
Output is correct |
43 |
Correct |
26 ms |
30700 KB |
Output is correct |
44 |
Correct |
26 ms |
30700 KB |
Output is correct |
45 |
Correct |
45 ms |
36588 KB |
Output is correct |
46 |
Correct |
46 ms |
36716 KB |
Output is correct |
47 |
Correct |
111 ms |
41264 KB |
Output is correct |
48 |
Correct |
34 ms |
35948 KB |
Output is correct |
49 |
Correct |
33 ms |
35948 KB |
Output is correct |
50 |
Correct |
31 ms |
36076 KB |
Output is correct |
51 |
Correct |
85 ms |
38448 KB |
Output is correct |
52 |
Correct |
91 ms |
38704 KB |
Output is correct |
53 |
Correct |
40 ms |
35052 KB |
Output is correct |
54 |
Correct |
27 ms |
34924 KB |
Output is correct |
55 |
Correct |
28 ms |
34924 KB |
Output is correct |
56 |
Correct |
38 ms |
35948 KB |
Output is correct |
57 |
Correct |
174 ms |
41460 KB |
Output is correct |
58 |
Correct |
37 ms |
35180 KB |
Output is correct |
59 |
Correct |
40 ms |
35436 KB |
Output is correct |
60 |
Correct |
44 ms |
36332 KB |
Output is correct |
61 |
Correct |
41 ms |
35692 KB |
Output is correct |
62 |
Correct |
67 ms |
39660 KB |
Output is correct |
63 |
Correct |
121 ms |
71204 KB |
Output is correct |
64 |
Correct |
142 ms |
84260 KB |
Output is correct |
65 |
Correct |
184 ms |
93464 KB |
Output is correct |
66 |
Correct |
430 ms |
118676 KB |
Output is correct |
67 |
Correct |
433 ms |
118136 KB |
Output is correct |