#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 3e4 + 5, S = sqrt(N) + 1;
struct Node{
int d, u, j;
Node(int d = 0, int u = 0, int j = 0): d(d), u(u), j(j){
}
};
bool operator< (const Node& lhs, const Node& rhs){
if (lhs.d != rhs.d) return lhs.d < rhs.d;
if (lhs.u != rhs.u) return lhs.u < rhs.u;
return lhs.j < rhs.j;
}
bool operator> (const Node& lhs, const Node& rhs){
if (lhs.d != rhs.d) return lhs.d > rhs.d;
if (lhs.u != rhs.u) return lhs.u > rhs.u;
return lhs.j > rhs.j;
}
int n, m;
int b[N], p[N];
vi pos[N]; // cac doge j co b_j = i
vpii adj[N]; // cac canh i ma p_i > S - 1
int dist[N][S]; // j = 1 -> S - 1: jump length j, j = 0: other jump
priority_queue <Node, vector <Node>, greater <Node>> pq;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n >> m;
For(i, 0, m){
cin >> b[i] >> p[i];
}
For(i, 0, m){
if (p[i] > S - 1){
int u, d;
u = b[i] + p[i]; d = 1;
while (u < n){
adj[b[i]].emplace_back(u, d);
u += p[i]; d++;
}
u = b[i] - p[i]; d = 1;
while (u >= 0){
adj[b[i]].emplace_back(u, d);
u -= p[i]; d++;
}
}
else{
pos[b[i]].emplace_back(p[i]);
}
}
memset(dist, 0x3f, sizeof(dist));
dist[b[0]][0] = 0;
pq.push(Node(dist[b[0]][0], b[0], 0));
while (!pq.empty()){
Node t = pq.top(); pq.pop();
if (dist[t.u][t.j] != t.d){
continue;
}
if (t.u == b[1]){
cout << t.d << endl;
return 0;
}
if (t.j){
if (dist[t.u][0] > t.d){
dist[t.u][0] = t.d;
pq.push(Node(dist[t.u][0], t.u, 0));
}
if (t.u + t.j < n and dist[t.u + t.j][t.j] > t.d + 1){
dist[t.u + t.j][t.j] = t.d + 1;
pq.push(Node(dist[t.u + t.j][t.j], t.u + t.j, t.j));
}
if (t.u - t.j >= 0 and dist[t.u - t.j][t.j] > t.d + 1){
dist[t.u - t.j][t.j] = t.d + 1;
pq.push(Node(dist[t.u - t.j][t.j], t.u - t.j, t.j));
}
}
else{
Fora(&edge, adj[t.u]){
if (dist[edge.fi][0] > t.d + edge.se){
dist[edge.fi][0] = t.d + edge.se;
pq.push(Node(dist[edge.fi][0], edge.fi, 0));
}
}
Fora(&idx, pos[t.u]){
if (dist[t.u][idx] > t.d){
dist[t.u][idx] = t.d;
pq.push(Node(dist[t.u][idx], t.u, idx));
}
}
}
}
cout << -1 << endl;
}
/*
n dinh, m con doge. doge thu i bat dau tu b_i, nhay sang trai/phai p_i buoc. hoi khoang cach nho nhat nhay tu doge 0 sang 1.
neu nhu p_i <= S - 2: bfs trong dist[u][p_i]
ngoai ra: nhay giua cac dinh vi co toi da N / S dinh den duoc
n * sqrt(n) * log ~ 5e6 * log, co the qua
co the toi uu thanh n * s duoc ko = bfs thay dijkstra???
cu thu dijkstra truoc thoi :pray:
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22092 KB |
Output is correct |
2 |
Correct |
10 ms |
22124 KB |
Output is correct |
3 |
Correct |
10 ms |
22152 KB |
Output is correct |
4 |
Correct |
10 ms |
22092 KB |
Output is correct |
5 |
Correct |
10 ms |
22092 KB |
Output is correct |
6 |
Correct |
10 ms |
22112 KB |
Output is correct |
7 |
Correct |
11 ms |
22188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22092 KB |
Output is correct |
2 |
Correct |
11 ms |
22092 KB |
Output is correct |
3 |
Correct |
10 ms |
22092 KB |
Output is correct |
4 |
Correct |
11 ms |
22116 KB |
Output is correct |
5 |
Correct |
11 ms |
22068 KB |
Output is correct |
6 |
Correct |
10 ms |
22092 KB |
Output is correct |
7 |
Correct |
13 ms |
22164 KB |
Output is correct |
8 |
Correct |
11 ms |
22084 KB |
Output is correct |
9 |
Correct |
11 ms |
22096 KB |
Output is correct |
10 |
Correct |
10 ms |
22092 KB |
Output is correct |
11 |
Correct |
11 ms |
22220 KB |
Output is correct |
12 |
Correct |
11 ms |
22192 KB |
Output is correct |
13 |
Correct |
11 ms |
22200 KB |
Output is correct |
14 |
Correct |
12 ms |
22264 KB |
Output is correct |
15 |
Correct |
12 ms |
22220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22092 KB |
Output is correct |
2 |
Correct |
12 ms |
22096 KB |
Output is correct |
3 |
Correct |
10 ms |
22092 KB |
Output is correct |
4 |
Correct |
11 ms |
22084 KB |
Output is correct |
5 |
Correct |
12 ms |
22100 KB |
Output is correct |
6 |
Correct |
11 ms |
22092 KB |
Output is correct |
7 |
Correct |
11 ms |
22092 KB |
Output is correct |
8 |
Correct |
11 ms |
22056 KB |
Output is correct |
9 |
Correct |
10 ms |
22092 KB |
Output is correct |
10 |
Correct |
10 ms |
22092 KB |
Output is correct |
11 |
Correct |
12 ms |
22168 KB |
Output is correct |
12 |
Correct |
13 ms |
22092 KB |
Output is correct |
13 |
Correct |
11 ms |
22172 KB |
Output is correct |
14 |
Correct |
11 ms |
22224 KB |
Output is correct |
15 |
Correct |
11 ms |
22220 KB |
Output is correct |
16 |
Correct |
11 ms |
22092 KB |
Output is correct |
17 |
Correct |
11 ms |
22196 KB |
Output is correct |
18 |
Correct |
10 ms |
22096 KB |
Output is correct |
19 |
Correct |
10 ms |
22092 KB |
Output is correct |
20 |
Correct |
11 ms |
22220 KB |
Output is correct |
21 |
Correct |
11 ms |
22096 KB |
Output is correct |
22 |
Correct |
11 ms |
22072 KB |
Output is correct |
23 |
Correct |
12 ms |
22144 KB |
Output is correct |
24 |
Correct |
12 ms |
22220 KB |
Output is correct |
25 |
Correct |
11 ms |
22216 KB |
Output is correct |
26 |
Correct |
12 ms |
22100 KB |
Output is correct |
27 |
Correct |
13 ms |
22132 KB |
Output is correct |
28 |
Correct |
13 ms |
22212 KB |
Output is correct |
29 |
Correct |
16 ms |
22088 KB |
Output is correct |
30 |
Correct |
12 ms |
22172 KB |
Output is correct |
31 |
Correct |
14 ms |
22204 KB |
Output is correct |
32 |
Correct |
13 ms |
22092 KB |
Output is correct |
33 |
Correct |
23 ms |
22220 KB |
Output is correct |
34 |
Correct |
18 ms |
22256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
22088 KB |
Output is correct |
2 |
Correct |
11 ms |
22092 KB |
Output is correct |
3 |
Correct |
11 ms |
22112 KB |
Output is correct |
4 |
Correct |
12 ms |
22092 KB |
Output is correct |
5 |
Correct |
11 ms |
22088 KB |
Output is correct |
6 |
Correct |
11 ms |
22128 KB |
Output is correct |
7 |
Correct |
10 ms |
22092 KB |
Output is correct |
8 |
Correct |
11 ms |
22092 KB |
Output is correct |
9 |
Correct |
10 ms |
22084 KB |
Output is correct |
10 |
Correct |
11 ms |
22076 KB |
Output is correct |
11 |
Correct |
11 ms |
22092 KB |
Output is correct |
12 |
Correct |
11 ms |
22124 KB |
Output is correct |
13 |
Correct |
11 ms |
22092 KB |
Output is correct |
14 |
Correct |
11 ms |
22220 KB |
Output is correct |
15 |
Correct |
12 ms |
22220 KB |
Output is correct |
16 |
Correct |
10 ms |
22092 KB |
Output is correct |
17 |
Correct |
11 ms |
22152 KB |
Output is correct |
18 |
Correct |
11 ms |
22100 KB |
Output is correct |
19 |
Correct |
10 ms |
22092 KB |
Output is correct |
20 |
Correct |
11 ms |
22228 KB |
Output is correct |
21 |
Correct |
11 ms |
22116 KB |
Output is correct |
22 |
Correct |
10 ms |
22092 KB |
Output is correct |
23 |
Correct |
11 ms |
22092 KB |
Output is correct |
24 |
Correct |
12 ms |
22220 KB |
Output is correct |
25 |
Correct |
11 ms |
22128 KB |
Output is correct |
26 |
Correct |
11 ms |
22100 KB |
Output is correct |
27 |
Correct |
12 ms |
22220 KB |
Output is correct |
28 |
Correct |
12 ms |
22220 KB |
Output is correct |
29 |
Correct |
16 ms |
22092 KB |
Output is correct |
30 |
Correct |
13 ms |
22088 KB |
Output is correct |
31 |
Correct |
14 ms |
22092 KB |
Output is correct |
32 |
Correct |
13 ms |
22196 KB |
Output is correct |
33 |
Correct |
23 ms |
22260 KB |
Output is correct |
34 |
Correct |
20 ms |
22208 KB |
Output is correct |
35 |
Correct |
21 ms |
22924 KB |
Output is correct |
36 |
Correct |
12 ms |
22220 KB |
Output is correct |
37 |
Correct |
18 ms |
23376 KB |
Output is correct |
38 |
Correct |
19 ms |
23380 KB |
Output is correct |
39 |
Correct |
19 ms |
23336 KB |
Output is correct |
40 |
Correct |
18 ms |
23328 KB |
Output is correct |
41 |
Correct |
19 ms |
23372 KB |
Output is correct |
42 |
Correct |
17 ms |
22732 KB |
Output is correct |
43 |
Correct |
16 ms |
22732 KB |
Output is correct |
44 |
Correct |
16 ms |
22752 KB |
Output is correct |
45 |
Correct |
65 ms |
24984 KB |
Output is correct |
46 |
Correct |
42 ms |
25064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
22092 KB |
Output is correct |
2 |
Correct |
10 ms |
22064 KB |
Output is correct |
3 |
Correct |
10 ms |
22124 KB |
Output is correct |
4 |
Correct |
11 ms |
22092 KB |
Output is correct |
5 |
Correct |
10 ms |
22088 KB |
Output is correct |
6 |
Correct |
11 ms |
22092 KB |
Output is correct |
7 |
Correct |
13 ms |
22044 KB |
Output is correct |
8 |
Correct |
11 ms |
22076 KB |
Output is correct |
9 |
Correct |
11 ms |
22092 KB |
Output is correct |
10 |
Correct |
11 ms |
22092 KB |
Output is correct |
11 |
Correct |
11 ms |
22092 KB |
Output is correct |
12 |
Correct |
11 ms |
22092 KB |
Output is correct |
13 |
Correct |
10 ms |
22092 KB |
Output is correct |
14 |
Correct |
13 ms |
22204 KB |
Output is correct |
15 |
Correct |
12 ms |
22136 KB |
Output is correct |
16 |
Correct |
11 ms |
22092 KB |
Output is correct |
17 |
Correct |
11 ms |
22220 KB |
Output is correct |
18 |
Correct |
11 ms |
22092 KB |
Output is correct |
19 |
Correct |
11 ms |
22092 KB |
Output is correct |
20 |
Correct |
11 ms |
22196 KB |
Output is correct |
21 |
Correct |
11 ms |
22092 KB |
Output is correct |
22 |
Correct |
11 ms |
22092 KB |
Output is correct |
23 |
Correct |
12 ms |
22172 KB |
Output is correct |
24 |
Correct |
14 ms |
22164 KB |
Output is correct |
25 |
Correct |
11 ms |
22188 KB |
Output is correct |
26 |
Correct |
11 ms |
22092 KB |
Output is correct |
27 |
Correct |
11 ms |
22100 KB |
Output is correct |
28 |
Correct |
12 ms |
22220 KB |
Output is correct |
29 |
Correct |
17 ms |
22092 KB |
Output is correct |
30 |
Correct |
12 ms |
22092 KB |
Output is correct |
31 |
Correct |
14 ms |
22092 KB |
Output is correct |
32 |
Correct |
13 ms |
22092 KB |
Output is correct |
33 |
Correct |
23 ms |
22252 KB |
Output is correct |
34 |
Correct |
19 ms |
22220 KB |
Output is correct |
35 |
Correct |
18 ms |
22988 KB |
Output is correct |
36 |
Correct |
12 ms |
22240 KB |
Output is correct |
37 |
Correct |
17 ms |
23320 KB |
Output is correct |
38 |
Correct |
19 ms |
23376 KB |
Output is correct |
39 |
Correct |
22 ms |
23232 KB |
Output is correct |
40 |
Correct |
18 ms |
23348 KB |
Output is correct |
41 |
Correct |
20 ms |
23332 KB |
Output is correct |
42 |
Correct |
16 ms |
22852 KB |
Output is correct |
43 |
Correct |
16 ms |
22748 KB |
Output is correct |
44 |
Correct |
16 ms |
22732 KB |
Output is correct |
45 |
Correct |
66 ms |
24984 KB |
Output is correct |
46 |
Correct |
40 ms |
25044 KB |
Output is correct |
47 |
Correct |
28 ms |
26788 KB |
Output is correct |
48 |
Correct |
18 ms |
23628 KB |
Output is correct |
49 |
Correct |
19 ms |
23380 KB |
Output is correct |
50 |
Correct |
15 ms |
23180 KB |
Output is correct |
51 |
Correct |
30 ms |
24852 KB |
Output is correct |
52 |
Correct |
30 ms |
24836 KB |
Output is correct |
53 |
Correct |
17 ms |
22848 KB |
Output is correct |
54 |
Correct |
11 ms |
22092 KB |
Output is correct |
55 |
Correct |
11 ms |
22072 KB |
Output is correct |
56 |
Correct |
21 ms |
23500 KB |
Output is correct |
57 |
Correct |
11 ms |
22176 KB |
Output is correct |
58 |
Correct |
18 ms |
22732 KB |
Output is correct |
59 |
Correct |
19 ms |
22860 KB |
Output is correct |
60 |
Correct |
19 ms |
23124 KB |
Output is correct |
61 |
Correct |
19 ms |
23036 KB |
Output is correct |
62 |
Correct |
48 ms |
25160 KB |
Output is correct |
63 |
Correct |
93 ms |
47760 KB |
Output is correct |
64 |
Correct |
97 ms |
55552 KB |
Output is correct |
65 |
Correct |
326 ms |
54136 KB |
Output is correct |
66 |
Correct |
865 ms |
49676 KB |
Output is correct |
67 |
Correct |
518 ms |
49652 KB |
Output is correct |