#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const int N = 200, inf = 1e9 + 7; //
#define node pair<pair<int, int>, int>
#define mode first.first
#define a first.second
#define b second
int n, m;
int f(node i){
if (i.mode == 0) return n * i.a + i.b;
else return N * i.a + i.b / N;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
int x[m], d[m];
bitset<201 * 30000> vis[2]{};
int dis[2][201 * 30000];
vector<int> on[n];
for (int i = 0; i < m; i++){
cin >> x[i] >> d[i];
on[x[i]].push_back(i);
}
deque<node> q;
if (d[0] < N) q.push_back({{0, d[0]}, x[0]});
else q.push_back({{1, 0}, x[0]});
vis[d[0] < N ? 0 : 1][f(q.front())] = true;
while (!q.empty()){
node u = q.front();
q.pop_front();
//cout << dis[u] << ' ' << u.a << ' ' << u.b << ' ' << '\n';
if (u.mode == 0){
if (u.b + u.a < n and !vis[0][f({{0, u.a}, u.b + u.a})]){
q.push_back({{0, u.a}, u.b + u.a});
dis[0][f({{0, u.a}, u.b + u.a})] = dis[0][f(u)] + 1;
vis[0][f({{0, u.a}, u.b + u.a})] = true;
}
if (u.b - u.a >= 0 and !vis[0][f({{0, u.a}, u.b - u.a})]){
q.push_back({{0, u.a}, u.b - u.a});
dis[0][f({{0, u.a}, u.b - u.a})] = dis[0][f(u)] + 1;
vis[0][f({{0, u.a}, u.b - u.a})] = true;
}
}
else {
if (u.b + d[u.a] < n and !vis[1][f({{1, u.a}, u.b + d[u.a]})]){
q.push_back({{1, u.a}, u.b + d[u.a]});
dis[1][f({{1, u.a}, u.b + d[u.a]})] = dis[1][f(u)] + 1;
vis[1][f({{1, u.a}, u.b + d[u.a]})] = true;
}
if (u.b - d[u.a] >= 0 and !vis[1][f({{1, u.a}, u.b - d[u.a]})]){
q.push_back({{1, u.a}, u.b - d[u.a]});
dis[1][f({{1, u.a}, u.b - d[u.a]})] = dis[1][f(u)] + 1;
vis[1][f({{1, u.a}, u.b - d[u.a]})] = true;
}
}
while (!on[u.b].empty()){
int v = on[u.b].back();
on[u.b].pop_back();
if (d[v] < N and !vis[0][f({{0, d[v]}, u.b})]){
dis[0][f({{0, d[v]}, u.b})] = dis[u.mode][f(u)];
vis[0][f({{0, d[v]}, u.b})] = true;
q.push_front({{0, d[v]}, u.b});
}
else if (d[v] >= N and !vis[1][f({{1, v}, u.b})]){
dis[1][f({{1, v}, u.b})] = dis[u.mode][f(u)];
vis[1][f({{1, v}, u.b})] = true;
q.push_front({{1, v}, u.b});
}
}
if (u.b == x[1]){
cout << dis[u.mode][f(u)];
return 0;
}
}
cout << -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
2 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
2 ms |
1772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
2 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
1 ms |
1900 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
1 ms |
1900 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
2 ms |
1772 KB |
Output is correct |
17 |
Correct |
3 ms |
3948 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1900 KB |
Output is correct |
22 |
Correct |
2 ms |
1900 KB |
Output is correct |
23 |
Correct |
3 ms |
3308 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
2 ms |
2924 KB |
Output is correct |
26 |
Correct |
2 ms |
2028 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
3 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
2028 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
5 ms |
2412 KB |
Output is correct |
34 |
Correct |
3 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
2 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
2 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1900 KB |
Output is correct |
10 |
Correct |
1 ms |
1900 KB |
Output is correct |
11 |
Correct |
2 ms |
2028 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1900 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
1 ms |
1772 KB |
Output is correct |
17 |
Correct |
3 ms |
3820 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
1900 KB |
Output is correct |
21 |
Correct |
2 ms |
1900 KB |
Output is correct |
22 |
Correct |
2 ms |
1900 KB |
Output is correct |
23 |
Correct |
2 ms |
3308 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
3 ms |
2924 KB |
Output is correct |
26 |
Correct |
2 ms |
2028 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
2028 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
5 ms |
2412 KB |
Output is correct |
34 |
Correct |
4 ms |
2412 KB |
Output is correct |
35 |
Correct |
21 ms |
23788 KB |
Output is correct |
36 |
Correct |
4 ms |
5356 KB |
Output is correct |
37 |
Correct |
15 ms |
18412 KB |
Output is correct |
38 |
Correct |
28 ms |
27628 KB |
Output is correct |
39 |
Correct |
10 ms |
2924 KB |
Output is correct |
40 |
Correct |
10 ms |
5612 KB |
Output is correct |
41 |
Correct |
16 ms |
16620 KB |
Output is correct |
42 |
Correct |
6 ms |
2412 KB |
Output is correct |
43 |
Correct |
7 ms |
3052 KB |
Output is correct |
44 |
Correct |
6 ms |
2412 KB |
Output is correct |
45 |
Correct |
33 ms |
25068 KB |
Output is correct |
46 |
Correct |
29 ms |
24684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
2 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
1 ms |
1900 KB |
Output is correct |
11 |
Correct |
2 ms |
1900 KB |
Output is correct |
12 |
Correct |
2 ms |
1900 KB |
Output is correct |
13 |
Correct |
2 ms |
1900 KB |
Output is correct |
14 |
Correct |
2 ms |
1920 KB |
Output is correct |
15 |
Correct |
2 ms |
1900 KB |
Output is correct |
16 |
Correct |
1 ms |
1772 KB |
Output is correct |
17 |
Correct |
3 ms |
3820 KB |
Output is correct |
18 |
Correct |
2 ms |
1900 KB |
Output is correct |
19 |
Correct |
2 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
2048 KB |
Output is correct |
21 |
Correct |
2 ms |
1900 KB |
Output is correct |
22 |
Correct |
2 ms |
1900 KB |
Output is correct |
23 |
Correct |
3 ms |
3308 KB |
Output is correct |
24 |
Correct |
3 ms |
4332 KB |
Output is correct |
25 |
Correct |
2 ms |
2924 KB |
Output is correct |
26 |
Correct |
2 ms |
2028 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
4 ms |
4332 KB |
Output is correct |
29 |
Correct |
3 ms |
2156 KB |
Output is correct |
30 |
Correct |
2 ms |
1900 KB |
Output is correct |
31 |
Correct |
2 ms |
2028 KB |
Output is correct |
32 |
Correct |
2 ms |
2028 KB |
Output is correct |
33 |
Correct |
4 ms |
2412 KB |
Output is correct |
34 |
Correct |
3 ms |
2412 KB |
Output is correct |
35 |
Correct |
21 ms |
23788 KB |
Output is correct |
36 |
Correct |
4 ms |
5356 KB |
Output is correct |
37 |
Correct |
15 ms |
18412 KB |
Output is correct |
38 |
Correct |
24 ms |
27628 KB |
Output is correct |
39 |
Correct |
8 ms |
2924 KB |
Output is correct |
40 |
Correct |
9 ms |
5484 KB |
Output is correct |
41 |
Correct |
16 ms |
16620 KB |
Output is correct |
42 |
Correct |
7 ms |
2412 KB |
Output is correct |
43 |
Correct |
6 ms |
3052 KB |
Output is correct |
44 |
Correct |
6 ms |
2412 KB |
Output is correct |
45 |
Correct |
34 ms |
24940 KB |
Output is correct |
46 |
Correct |
29 ms |
24684 KB |
Output is correct |
47 |
Correct |
16 ms |
15212 KB |
Output is correct |
48 |
Correct |
8 ms |
3052 KB |
Output is correct |
49 |
Correct |
7 ms |
3052 KB |
Output is correct |
50 |
Correct |
6 ms |
2924 KB |
Output is correct |
51 |
Correct |
30 ms |
27756 KB |
Output is correct |
52 |
Correct |
33 ms |
28012 KB |
Output is correct |
53 |
Correct |
18 ms |
15084 KB |
Output is correct |
54 |
Correct |
2 ms |
2668 KB |
Output is correct |
55 |
Correct |
2 ms |
2796 KB |
Output is correct |
56 |
Correct |
10 ms |
3820 KB |
Output is correct |
57 |
Correct |
2 ms |
2668 KB |
Output is correct |
58 |
Correct |
8 ms |
4588 KB |
Output is correct |
59 |
Correct |
9 ms |
5228 KB |
Output is correct |
60 |
Correct |
11 ms |
5228 KB |
Output is correct |
61 |
Correct |
13 ms |
7020 KB |
Output is correct |
62 |
Correct |
45 ms |
37740 KB |
Output is correct |
63 |
Correct |
166 ms |
26860 KB |
Output is correct |
64 |
Correct |
216 ms |
26988 KB |
Output is correct |
65 |
Correct |
286 ms |
39020 KB |
Output is correct |
66 |
Correct |
276 ms |
48492 KB |
Output is correct |
67 |
Correct |
201 ms |
35600 KB |
Output is correct |