#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define SZ(x) ll(x.size())
const ll MAXN = 3e4 + 10;
const ll SQ = 200;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; //998244353; //1e9 + 9;
int n , m , ptr;
int q[MAXN * SQ * 4] , dist[MAXN * SQ * 2] , pos[MAXN * SQ * 2] , len[MAXN * SQ * 2];
vector<int> adj[MAXN];
void BFS(int v){
fill(dist , dist + MAXN * SQ * 2 , MOD);
dist[v] = 0;
int ql = MAXN * SQ * 2 , qr = ql;
q[qr++] = v;
while(ql < qr){
int v = q[ql++];
if(v < n){
for(int u : adj[v]){
if(dist[u] > dist[v]){
dist[u] = dist[v];
q[--ql] = u;
}
}
continue;
}
if(dist[pos[v]] > dist[v]){
dist[pos[v]] = dist[v];
q[--ql] = pos[v];
}
if(v < n + m) continue;
for(int i = -1 ; i <= 1 ; i += 2){
int u = v + i * (len[v] < 0 ? -1 : len[v]) , p = pos[v] + i * len[v];
if(p < 0 || p >= n) continue;
if(dist[u] > dist[v] + 1){
dist[u] = dist[v] + 1;
q[qr++] = u;
}
}
}
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n >> m;
ptr = n + m;
for(int i = 1 ; i < SQ ; i++){
for(int j = 0 ; j < n ; j++){
pos[ptr + j] = j;
len[ptr + j] = i;
}
ptr += n;
}
for(int i = 0 ; i < m ; i++){
int x , y;
cin >> x >> y;
adj[x].push_back(n + i);
if(y < SQ){
pos[n + i] = m + n * y + x;
continue;
}
int z = x % y;
for(int j = z ; j < n ; j += y){
pos[ptr] = j;
len[ptr] = -y;
if(j == x){
pos[n + i] = ptr;
}
ptr++;
}
}
BFS(n);
cout << (dist[n + 1] == MOD ? -1 : dist[n + 1]) << endl;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
48108 KB |
Output is correct |
2 |
Correct |
33 ms |
48108 KB |
Output is correct |
3 |
Correct |
33 ms |
48108 KB |
Output is correct |
4 |
Correct |
31 ms |
48108 KB |
Output is correct |
5 |
Correct |
33 ms |
48108 KB |
Output is correct |
6 |
Correct |
33 ms |
48108 KB |
Output is correct |
7 |
Correct |
32 ms |
48140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
48108 KB |
Output is correct |
2 |
Correct |
33 ms |
48108 KB |
Output is correct |
3 |
Correct |
31 ms |
48108 KB |
Output is correct |
4 |
Correct |
30 ms |
48108 KB |
Output is correct |
5 |
Correct |
33 ms |
48108 KB |
Output is correct |
6 |
Correct |
31 ms |
48108 KB |
Output is correct |
7 |
Correct |
32 ms |
48108 KB |
Output is correct |
8 |
Correct |
31 ms |
48108 KB |
Output is correct |
9 |
Correct |
32 ms |
48312 KB |
Output is correct |
10 |
Correct |
33 ms |
48236 KB |
Output is correct |
11 |
Correct |
34 ms |
48236 KB |
Output is correct |
12 |
Correct |
33 ms |
48240 KB |
Output is correct |
13 |
Correct |
31 ms |
48236 KB |
Output is correct |
14 |
Correct |
34 ms |
48236 KB |
Output is correct |
15 |
Correct |
32 ms |
48236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
48108 KB |
Output is correct |
2 |
Correct |
30 ms |
48108 KB |
Output is correct |
3 |
Correct |
33 ms |
48108 KB |
Output is correct |
4 |
Correct |
35 ms |
48108 KB |
Output is correct |
5 |
Correct |
35 ms |
48108 KB |
Output is correct |
6 |
Correct |
30 ms |
48108 KB |
Output is correct |
7 |
Correct |
31 ms |
48108 KB |
Output is correct |
8 |
Correct |
31 ms |
48108 KB |
Output is correct |
9 |
Correct |
32 ms |
48108 KB |
Output is correct |
10 |
Correct |
78 ms |
48204 KB |
Output is correct |
11 |
Correct |
78 ms |
48236 KB |
Output is correct |
12 |
Correct |
78 ms |
48220 KB |
Output is correct |
13 |
Correct |
32 ms |
48236 KB |
Output is correct |
14 |
Correct |
32 ms |
48236 KB |
Output is correct |
15 |
Correct |
33 ms |
48236 KB |
Output is correct |
16 |
Correct |
31 ms |
48384 KB |
Output is correct |
17 |
Correct |
37 ms |
49388 KB |
Output is correct |
18 |
Correct |
34 ms |
51008 KB |
Output is correct |
19 |
Correct |
86 ms |
51308 KB |
Output is correct |
20 |
Correct |
40 ms |
51336 KB |
Output is correct |
21 |
Correct |
33 ms |
48688 KB |
Output is correct |
22 |
Correct |
35 ms |
50936 KB |
Output is correct |
23 |
Correct |
34 ms |
50668 KB |
Output is correct |
24 |
Correct |
39 ms |
51180 KB |
Output is correct |
25 |
Correct |
38 ms |
51308 KB |
Output is correct |
26 |
Correct |
35 ms |
51308 KB |
Output is correct |
27 |
Correct |
36 ms |
51180 KB |
Output is correct |
28 |
Correct |
35 ms |
51424 KB |
Output is correct |
29 |
Correct |
35 ms |
51436 KB |
Output is correct |
30 |
Correct |
35 ms |
51180 KB |
Output is correct |
31 |
Correct |
34 ms |
51308 KB |
Output is correct |
32 |
Correct |
276 ms |
51308 KB |
Output is correct |
33 |
Correct |
37 ms |
51692 KB |
Output is correct |
34 |
Correct |
38 ms |
51692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48112 KB |
Output is correct |
2 |
Correct |
31 ms |
48108 KB |
Output is correct |
3 |
Correct |
32 ms |
48108 KB |
Output is correct |
4 |
Correct |
74 ms |
48100 KB |
Output is correct |
5 |
Correct |
77 ms |
48108 KB |
Output is correct |
6 |
Correct |
78 ms |
48116 KB |
Output is correct |
7 |
Correct |
32 ms |
48108 KB |
Output is correct |
8 |
Correct |
77 ms |
48108 KB |
Output is correct |
9 |
Correct |
32 ms |
48108 KB |
Output is correct |
10 |
Correct |
78 ms |
48264 KB |
Output is correct |
11 |
Correct |
33 ms |
48236 KB |
Output is correct |
12 |
Correct |
32 ms |
48236 KB |
Output is correct |
13 |
Correct |
35 ms |
48236 KB |
Output is correct |
14 |
Correct |
33 ms |
48236 KB |
Output is correct |
15 |
Correct |
32 ms |
48236 KB |
Output is correct |
16 |
Correct |
32 ms |
48364 KB |
Output is correct |
17 |
Correct |
35 ms |
49392 KB |
Output is correct |
18 |
Correct |
34 ms |
50796 KB |
Output is correct |
19 |
Correct |
38 ms |
51180 KB |
Output is correct |
20 |
Correct |
37 ms |
51252 KB |
Output is correct |
21 |
Correct |
34 ms |
48748 KB |
Output is correct |
22 |
Correct |
35 ms |
50884 KB |
Output is correct |
23 |
Correct |
37 ms |
50668 KB |
Output is correct |
24 |
Correct |
35 ms |
51180 KB |
Output is correct |
25 |
Correct |
35 ms |
51308 KB |
Output is correct |
26 |
Correct |
36 ms |
51216 KB |
Output is correct |
27 |
Correct |
81 ms |
51308 KB |
Output is correct |
28 |
Correct |
35 ms |
51436 KB |
Output is correct |
29 |
Correct |
36 ms |
51436 KB |
Output is correct |
30 |
Correct |
37 ms |
51180 KB |
Output is correct |
31 |
Correct |
360 ms |
51344 KB |
Output is correct |
32 |
Correct |
34 ms |
51308 KB |
Output is correct |
33 |
Correct |
36 ms |
51692 KB |
Output is correct |
34 |
Correct |
457 ms |
51620 KB |
Output is correct |
35 |
Correct |
44 ms |
51524 KB |
Output is correct |
36 |
Correct |
39 ms |
50028 KB |
Output is correct |
37 |
Correct |
139 ms |
52600 KB |
Output is correct |
38 |
Correct |
52 ms |
52820 KB |
Output is correct |
39 |
Correct |
50 ms |
52844 KB |
Output is correct |
40 |
Correct |
96 ms |
52844 KB |
Output is correct |
41 |
Correct |
98 ms |
52972 KB |
Output is correct |
42 |
Correct |
84 ms |
51948 KB |
Output is correct |
43 |
Correct |
88 ms |
51948 KB |
Output is correct |
44 |
Correct |
42 ms |
51908 KB |
Output is correct |
45 |
Correct |
54 ms |
54252 KB |
Output is correct |
46 |
Correct |
54 ms |
54252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48108 KB |
Output is correct |
2 |
Correct |
32 ms |
48108 KB |
Output is correct |
3 |
Correct |
33 ms |
48108 KB |
Output is correct |
4 |
Correct |
72 ms |
48124 KB |
Output is correct |
5 |
Correct |
81 ms |
48108 KB |
Output is correct |
6 |
Correct |
32 ms |
48108 KB |
Output is correct |
7 |
Correct |
31 ms |
48108 KB |
Output is correct |
8 |
Correct |
31 ms |
48108 KB |
Output is correct |
9 |
Correct |
31 ms |
48108 KB |
Output is correct |
10 |
Correct |
35 ms |
48236 KB |
Output is correct |
11 |
Correct |
35 ms |
48236 KB |
Output is correct |
12 |
Correct |
39 ms |
48236 KB |
Output is correct |
13 |
Correct |
35 ms |
48236 KB |
Output is correct |
14 |
Correct |
32 ms |
48236 KB |
Output is correct |
15 |
Correct |
32 ms |
48236 KB |
Output is correct |
16 |
Correct |
31 ms |
48364 KB |
Output is correct |
17 |
Correct |
125 ms |
49212 KB |
Output is correct |
18 |
Correct |
79 ms |
50812 KB |
Output is correct |
19 |
Correct |
34 ms |
51180 KB |
Output is correct |
20 |
Correct |
35 ms |
51228 KB |
Output is correct |
21 |
Correct |
133 ms |
48640 KB |
Output is correct |
22 |
Correct |
215 ms |
50840 KB |
Output is correct |
23 |
Correct |
227 ms |
50692 KB |
Output is correct |
24 |
Correct |
36 ms |
51180 KB |
Output is correct |
25 |
Correct |
35 ms |
51308 KB |
Output is correct |
26 |
Correct |
36 ms |
51196 KB |
Output is correct |
27 |
Correct |
36 ms |
51180 KB |
Output is correct |
28 |
Correct |
36 ms |
51412 KB |
Output is correct |
29 |
Correct |
37 ms |
51436 KB |
Output is correct |
30 |
Correct |
35 ms |
51184 KB |
Output is correct |
31 |
Correct |
124 ms |
51308 KB |
Output is correct |
32 |
Correct |
36 ms |
51296 KB |
Output is correct |
33 |
Correct |
38 ms |
51692 KB |
Output is correct |
34 |
Correct |
37 ms |
51692 KB |
Output is correct |
35 |
Correct |
46 ms |
51560 KB |
Output is correct |
36 |
Correct |
35 ms |
49940 KB |
Output is correct |
37 |
Correct |
47 ms |
52588 KB |
Output is correct |
38 |
Correct |
52 ms |
52844 KB |
Output is correct |
39 |
Correct |
49 ms |
52844 KB |
Output is correct |
40 |
Correct |
61 ms |
52844 KB |
Output is correct |
41 |
Correct |
61 ms |
52844 KB |
Output is correct |
42 |
Correct |
38 ms |
51820 KB |
Output is correct |
43 |
Correct |
38 ms |
51820 KB |
Output is correct |
44 |
Correct |
40 ms |
51820 KB |
Output is correct |
45 |
Correct |
52 ms |
54252 KB |
Output is correct |
46 |
Correct |
54 ms |
54240 KB |
Output is correct |
47 |
Correct |
112 ms |
73196 KB |
Output is correct |
48 |
Correct |
62 ms |
85868 KB |
Output is correct |
49 |
Correct |
64 ms |
89068 KB |
Output is correct |
50 |
Correct |
65 ms |
92696 KB |
Output is correct |
51 |
Correct |
93 ms |
98156 KB |
Output is correct |
52 |
Correct |
96 ms |
98284 KB |
Output is correct |
53 |
Correct |
77 ms |
96492 KB |
Output is correct |
54 |
Correct |
61 ms |
94828 KB |
Output is correct |
55 |
Correct |
61 ms |
94828 KB |
Output is correct |
56 |
Correct |
70 ms |
96236 KB |
Output is correct |
57 |
Correct |
76 ms |
96748 KB |
Output is correct |
58 |
Correct |
67 ms |
95744 KB |
Output is correct |
59 |
Correct |
68 ms |
95724 KB |
Output is correct |
60 |
Correct |
72 ms |
96108 KB |
Output is correct |
61 |
Correct |
77 ms |
95980 KB |
Output is correct |
62 |
Correct |
82 ms |
99692 KB |
Output is correct |
63 |
Correct |
314 ms |
121836 KB |
Output is correct |
64 |
Correct |
406 ms |
127852 KB |
Output is correct |
65 |
Correct |
551 ms |
130028 KB |
Output is correct |
66 |
Correct |
458 ms |
134380 KB |
Output is correct |
67 |
Correct |
429 ms |
134252 KB |
Output is correct |