#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
const int INF = 1e9;
int B = 500;
const int MAXN = 3e4 + 5;
vector<pair<int, int> > d;
int dist[MAXN][300];
vector<short> sm[MAXN];
vector<short> bg[MAXN];
signed main()
{
//ifstream cin("input.txt");
//ofstream cout("output.txt");
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
B = (int)sqrt(n);
d.resize(m);
vector<int> big;
for (int i = 0; i < m; i++)
{
cin >> d[i].first >> d[i].second;
if (d[i].second >= B)
{
big.push_back(i);
bg[d[i].first].push_back(d[i].second);
}
else
{
sm[d[i].first].push_back(d[i].second);
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < B; j++)
dist[i][j] = INF;
priority_queue<array<int, 3> > st;
st.push({0, d[0].first, 0});
dist[d[0].first][0] = 0;
while (!st.empty())
{
auto [dst, pos, pw] = st.top();
dst = -dst;
st.pop();
if (dst > dist[pos][pw]) continue;
if (dst != dist[pos][pw]) continue;
if (pw > 0)
{
vector<array<short, 3> > to;
if (pos - pw >= 0)
{
if (dist[pos - pw][pw] > dst + 1)
{
//st.erase({dist[pos - pw][pw], pos - pw, pw});
dist[pos - pw][pw] = dst + 1;
st.push({-dist[pos - pw][pw], pos - pw, pw});
}
if (dist[pos - pw][0] > dst + 1)
{
//st.erase({dist[pos - pw][0], pos - pw, 0});
dist[pos - pw][0] = dst + 1;
st.push({-dist[pos - pw][0], pos - pw, 0});
}
}
if (pos + pw < n)
{
if (dist[pos + pw][pw] > dst + 1)
{
//st.erase({dist[pos + pw][pw], pos + pw, pw});
dist[pos + pw][pw] = dst + 1;
st.push({-dist[pos + pw][pw], pos + pw, pw});
}
if (dist[pos + pw][0] > dst + 1)
{
//st.erase({dist[pos + pw][0], pos + pw, 0});
dist[pos + pw][0] = dst + 1;
st.push({-dist[pos + pw][0], pos + pw, 0});
}
}
continue;
}
for (auto& mv : bg[pos])
{
for (int j = pos, k = 0; j < n; j += mv, k++)
{
if (dist[j][0] > dst + k)
{
dist[j][0] = dst + k;
st.push({-dist[j][0], j, 0});
}
}
for (int j = pos, k = 0; j >= 0; j -= mv, k++)
{
if (dist[j][0] > dst + k)
{
dist[j][0] = dst + k;
st.push({-dist[j][0], j, 0});
}
}
}
for (auto& mv : sm[pos])
{
if (dist[pos][mv] > dst + 0)
{
dist[pos][mv] = dst + 0;
st.push({-dist[pos][mv], pos, mv});
}
}
}
int mn = INF;
for (int i = 0; i < B; i++)
mn = min(mn, dist[d[1].first][i]);
if (mn == INF)
cout << -1;
else
cout << mn;
return 0;
}
/*
small
(i, j) -1> (i - j, 0), (i + j, 0)
(i, 0) -0> (i, j)
(i, j) -1> (i - j, j), (i + j, j)
large
(i, 0) -1> (i + j, 0)
(i, 0) -1> (i - j, 0)
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1632 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1876 KB |
Output is correct |
13 |
Correct |
1 ms |
1748 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
2 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1748 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
3 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
3796 KB |
Output is correct |
19 |
Correct |
2 ms |
4052 KB |
Output is correct |
20 |
Correct |
2 ms |
4052 KB |
Output is correct |
21 |
Correct |
1 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
3796 KB |
Output is correct |
23 |
Correct |
3 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
4052 KB |
Output is correct |
25 |
Correct |
3 ms |
4052 KB |
Output is correct |
26 |
Correct |
3 ms |
4052 KB |
Output is correct |
27 |
Correct |
3 ms |
4052 KB |
Output is correct |
28 |
Correct |
3 ms |
4052 KB |
Output is correct |
29 |
Correct |
8 ms |
4052 KB |
Output is correct |
30 |
Correct |
4 ms |
4052 KB |
Output is correct |
31 |
Correct |
5 ms |
4136 KB |
Output is correct |
32 |
Correct |
3 ms |
4140 KB |
Output is correct |
33 |
Correct |
12 ms |
4216 KB |
Output is correct |
34 |
Correct |
12 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
1 ms |
1620 KB |
Output is correct |
4 |
Correct |
1 ms |
1620 KB |
Output is correct |
5 |
Correct |
1 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
1620 KB |
Output is correct |
7 |
Correct |
1 ms |
1620 KB |
Output is correct |
8 |
Correct |
1 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1748 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
1 ms |
1876 KB |
Output is correct |
12 |
Correct |
1 ms |
1816 KB |
Output is correct |
13 |
Correct |
1 ms |
1876 KB |
Output is correct |
14 |
Correct |
1 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
1 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
3796 KB |
Output is correct |
19 |
Correct |
2 ms |
4052 KB |
Output is correct |
20 |
Correct |
2 ms |
4052 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
3796 KB |
Output is correct |
23 |
Correct |
2 ms |
3540 KB |
Output is correct |
24 |
Correct |
3 ms |
4052 KB |
Output is correct |
25 |
Correct |
3 ms |
4052 KB |
Output is correct |
26 |
Correct |
3 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
4052 KB |
Output is correct |
28 |
Correct |
3 ms |
4064 KB |
Output is correct |
29 |
Correct |
8 ms |
4052 KB |
Output is correct |
30 |
Correct |
3 ms |
4052 KB |
Output is correct |
31 |
Correct |
5 ms |
4052 KB |
Output is correct |
32 |
Correct |
3 ms |
4052 KB |
Output is correct |
33 |
Correct |
11 ms |
4212 KB |
Output is correct |
34 |
Correct |
11 ms |
4220 KB |
Output is correct |
35 |
Correct |
10 ms |
3920 KB |
Output is correct |
36 |
Correct |
3 ms |
3028 KB |
Output is correct |
37 |
Correct |
12 ms |
4436 KB |
Output is correct |
38 |
Correct |
14 ms |
4612 KB |
Output is correct |
39 |
Correct |
13 ms |
4692 KB |
Output is correct |
40 |
Correct |
15 ms |
4716 KB |
Output is correct |
41 |
Correct |
14 ms |
4704 KB |
Output is correct |
42 |
Correct |
6 ms |
4436 KB |
Output is correct |
43 |
Correct |
6 ms |
4436 KB |
Output is correct |
44 |
Correct |
7 ms |
4436 KB |
Output is correct |
45 |
Correct |
16 ms |
4556 KB |
Output is correct |
46 |
Correct |
16 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Correct |
2 ms |
1624 KB |
Output is correct |
4 |
Correct |
2 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1624 KB |
Output is correct |
6 |
Correct |
1 ms |
1748 KB |
Output is correct |
7 |
Correct |
1 ms |
1632 KB |
Output is correct |
8 |
Correct |
1 ms |
1760 KB |
Output is correct |
9 |
Correct |
1 ms |
1760 KB |
Output is correct |
10 |
Correct |
2 ms |
1760 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
2 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
1876 KB |
Output is correct |
15 |
Correct |
1 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
3 ms |
2684 KB |
Output is correct |
18 |
Correct |
2 ms |
3796 KB |
Output is correct |
19 |
Correct |
2 ms |
4052 KB |
Output is correct |
20 |
Correct |
3 ms |
4052 KB |
Output is correct |
21 |
Correct |
2 ms |
2132 KB |
Output is correct |
22 |
Correct |
2 ms |
3796 KB |
Output is correct |
23 |
Correct |
3 ms |
3540 KB |
Output is correct |
24 |
Correct |
4 ms |
4052 KB |
Output is correct |
25 |
Correct |
3 ms |
4052 KB |
Output is correct |
26 |
Correct |
3 ms |
4048 KB |
Output is correct |
27 |
Correct |
2 ms |
4052 KB |
Output is correct |
28 |
Correct |
3 ms |
4064 KB |
Output is correct |
29 |
Correct |
8 ms |
4064 KB |
Output is correct |
30 |
Correct |
4 ms |
4052 KB |
Output is correct |
31 |
Correct |
6 ms |
4052 KB |
Output is correct |
32 |
Correct |
4 ms |
4180 KB |
Output is correct |
33 |
Correct |
11 ms |
4144 KB |
Output is correct |
34 |
Correct |
11 ms |
4180 KB |
Output is correct |
35 |
Correct |
11 ms |
3884 KB |
Output is correct |
36 |
Correct |
3 ms |
3028 KB |
Output is correct |
37 |
Correct |
12 ms |
4436 KB |
Output is correct |
38 |
Correct |
15 ms |
4692 KB |
Output is correct |
39 |
Correct |
14 ms |
4692 KB |
Output is correct |
40 |
Correct |
14 ms |
4692 KB |
Output is correct |
41 |
Correct |
14 ms |
4692 KB |
Output is correct |
42 |
Correct |
6 ms |
4436 KB |
Output is correct |
43 |
Correct |
5 ms |
4436 KB |
Output is correct |
44 |
Correct |
5 ms |
4436 KB |
Output is correct |
45 |
Correct |
14 ms |
4564 KB |
Output is correct |
46 |
Correct |
15 ms |
4692 KB |
Output is correct |
47 |
Correct |
80 ms |
17308 KB |
Output is correct |
48 |
Correct |
16 ms |
29632 KB |
Output is correct |
49 |
Correct |
17 ms |
32216 KB |
Output is correct |
50 |
Correct |
19 ms |
35032 KB |
Output is correct |
51 |
Correct |
54 ms |
38380 KB |
Output is correct |
52 |
Correct |
62 ms |
38348 KB |
Output is correct |
53 |
Correct |
31 ms |
37844 KB |
Output is correct |
54 |
Correct |
15 ms |
36948 KB |
Output is correct |
55 |
Correct |
15 ms |
36948 KB |
Output is correct |
56 |
Correct |
24 ms |
38140 KB |
Output is correct |
57 |
Correct |
92 ms |
37052 KB |
Output is correct |
58 |
Correct |
20 ms |
37296 KB |
Output is correct |
59 |
Correct |
24 ms |
37196 KB |
Output is correct |
60 |
Correct |
31 ms |
37332 KB |
Output is correct |
61 |
Correct |
25 ms |
37236 KB |
Output is correct |
62 |
Correct |
48 ms |
38228 KB |
Output is correct |
63 |
Correct |
49 ms |
38352 KB |
Output is correct |
64 |
Correct |
51 ms |
38276 KB |
Output is correct |
65 |
Correct |
272 ms |
39088 KB |
Output is correct |
66 |
Correct |
740 ms |
39048 KB |
Output is correct |
67 |
Correct |
747 ms |
39236 KB |
Output is correct |