#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll n, m;
cin >> n >> m;
ll b[m], p[m];
vector<ll> eq[n];
for (ll i = 0; i < m; i++)
cin >> b[i] >> p[i], eq[b[i]].push_back(p[i]);
for (ll i = 0; i < n; i++)
sort(eq[i].begin(), eq[i].end(), greater());
map<ll, ll> dist[n];
dist[b[0]][p[0]] = 0;
priority_queue<pair<ll, pair<ll, ll>>> q;
q.push(make_pair(0, make_pair(p[0], b[0])));
while (!q.empty())
{
auto [dst, x] = q.top();
auto [j, i] = x;
dst = -dst;
q.pop();
if (dst > dist[i][j]) continue;
if (i == b[1])
{
cout << dst << endl;
return;
}
if (i - j >= 0 and !dist[i - j].count(j) and dist[i - j].size() <= 5000)
dist[i - j][j] = dst + 1, q.push(make_pair(-(dst + 1), make_pair(j, i - j)));
if (i + j < n and !dist[i + j].count(j) and dist[i + j].size() <= 5000)
dist[i + j][j] = dst + 1, q.push(make_pair(-(dst + 1), make_pair(j, i + j)));
for (ll k : eq[i])
{
if (i - k >= 0 and !dist[i - k].count(k) and dist[i - k].size() <= 5000)
dist[i - k][k] = dst + 1, q.push(make_pair(-(dst + 1), make_pair(k, i - k)));
if (i + k < n and !dist[i + k].count(k) and dist[i + k].size() <= 5000)
dist[i + k][k] = dst + 1, q.push(make_pair(-(dst + 1), make_pair(k, i + k)));
}
eq[i].clear();
if (!dist[b[1]].empty())
{
ll ans = 1e18;
for (auto j : dist[b[1]])
ans = min(ans, j.second);
cout << ans << endl;
return;
}
}
cout << "-1\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll i = 1; i <= t; i++)
solve();
cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
448 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
548 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
980 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
4 ms |
1884 KB |
Output is correct |
29 |
Correct |
18 ms |
4300 KB |
Output is correct |
30 |
Correct |
4 ms |
1628 KB |
Output is correct |
31 |
Correct |
12 ms |
2652 KB |
Output is correct |
32 |
Correct |
7 ms |
1884 KB |
Output is correct |
33 |
Correct |
32 ms |
8172 KB |
Output is correct |
34 |
Correct |
16 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
356 KB |
Output is correct |
17 |
Correct |
1 ms |
612 KB |
Output is correct |
18 |
Correct |
1 ms |
612 KB |
Output is correct |
19 |
Correct |
1 ms |
612 KB |
Output is correct |
20 |
Correct |
1 ms |
612 KB |
Output is correct |
21 |
Correct |
0 ms |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
612 KB |
Output is correct |
23 |
Correct |
2 ms |
708 KB |
Output is correct |
24 |
Correct |
2 ms |
868 KB |
Output is correct |
25 |
Correct |
1 ms |
612 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
868 KB |
Output is correct |
28 |
Correct |
4 ms |
1892 KB |
Output is correct |
29 |
Correct |
23 ms |
4456 KB |
Output is correct |
30 |
Correct |
4 ms |
1640 KB |
Output is correct |
31 |
Correct |
9 ms |
2664 KB |
Output is correct |
32 |
Correct |
6 ms |
1896 KB |
Output is correct |
33 |
Correct |
32 ms |
8032 KB |
Output is correct |
34 |
Correct |
17 ms |
4556 KB |
Output is correct |
35 |
Correct |
6 ms |
2400 KB |
Output is correct |
36 |
Correct |
2 ms |
612 KB |
Output is correct |
37 |
Correct |
4 ms |
1632 KB |
Output is correct |
38 |
Correct |
6 ms |
1896 KB |
Output is correct |
39 |
Correct |
5 ms |
1640 KB |
Output is correct |
40 |
Correct |
6 ms |
1640 KB |
Output is correct |
41 |
Correct |
6 ms |
1628 KB |
Output is correct |
42 |
Correct |
4 ms |
1500 KB |
Output is correct |
43 |
Correct |
4 ms |
1752 KB |
Output is correct |
44 |
Correct |
5 ms |
1664 KB |
Output is correct |
45 |
Correct |
262 ms |
32940 KB |
Output is correct |
46 |
Correct |
107 ms |
17924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
860 KB |
Output is correct |
28 |
Correct |
5 ms |
1884 KB |
Output is correct |
29 |
Correct |
17 ms |
4444 KB |
Output is correct |
30 |
Correct |
4 ms |
1624 KB |
Output is correct |
31 |
Correct |
9 ms |
2652 KB |
Output is correct |
32 |
Correct |
6 ms |
1884 KB |
Output is correct |
33 |
Correct |
32 ms |
8020 KB |
Output is correct |
34 |
Correct |
16 ms |
4692 KB |
Output is correct |
35 |
Correct |
6 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
584 KB |
Output is correct |
37 |
Correct |
6 ms |
1368 KB |
Output is correct |
38 |
Correct |
6 ms |
1884 KB |
Output is correct |
39 |
Correct |
5 ms |
1628 KB |
Output is correct |
40 |
Correct |
5 ms |
1624 KB |
Output is correct |
41 |
Correct |
6 ms |
1560 KB |
Output is correct |
42 |
Correct |
4 ms |
1500 KB |
Output is correct |
43 |
Correct |
4 ms |
1756 KB |
Output is correct |
44 |
Correct |
4 ms |
1544 KB |
Output is correct |
45 |
Correct |
266 ms |
33072 KB |
Output is correct |
46 |
Correct |
127 ms |
17928 KB |
Output is correct |
47 |
Correct |
6 ms |
2908 KB |
Output is correct |
48 |
Correct |
9 ms |
3160 KB |
Output is correct |
49 |
Correct |
6 ms |
3164 KB |
Output is correct |
50 |
Correct |
5 ms |
3164 KB |
Output is correct |
51 |
Correct |
17 ms |
6596 KB |
Output is correct |
52 |
Correct |
21 ms |
6744 KB |
Output is correct |
53 |
Correct |
8 ms |
4188 KB |
Output is correct |
54 |
Correct |
3 ms |
3420 KB |
Output is correct |
55 |
Correct |
3 ms |
3932 KB |
Output is correct |
56 |
Correct |
9 ms |
5980 KB |
Output is correct |
57 |
Correct |
2 ms |
2688 KB |
Output is correct |
58 |
Correct |
13 ms |
7500 KB |
Output is correct |
59 |
Correct |
11 ms |
7136 KB |
Output is correct |
60 |
Correct |
11 ms |
7000 KB |
Output is correct |
61 |
Correct |
11 ms |
6620 KB |
Output is correct |
62 |
Correct |
145 ms |
31256 KB |
Output is correct |
63 |
Execution timed out |
1061 ms |
85592 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |