#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
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());
if (rng() & 1)
swap(b[0], b[1]), swap(p[0], p[1]);
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() <= 1500)
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() <= 1500)
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() <= 1500)
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() <= 1500)
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";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
508 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
500 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |