#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) (x).begin(), (x).end()
const int mxn = 30010;
int n, m;
unordered_map<int, ll> best[mxn];
int spot[mxn], jump[mxn], val[mxn];
int dx[] = {-1, 1};
void solve() {
priority_queue<pair<ll, pair<int, int>>> pq;
pq.push({0, {spot[0], jump[0]}});
best[spot[0]][jump[0]] = 0;
while (pq.size()) {
ll dist = -pq.top().first, pos = pq.top().second.first, pwr = pq.top().second.second;
pq.pop();
if (dist > best[pos][pwr]) continue;
if (val[pos] != -1) {
if (!best[pos].count(jump[val[pos]])) best[pos][jump[val[pos]]] = 1e16;
if (dist < best[pos][jump[val[pos]]]) {
best[pos][jump[val[pos]]] = dist;
pq.push({-dist, {pos, jump[val[pos]]}});
}
}
for (int d = 0; d < 2; d++) {
int newPos = pos + pwr * dx[d];
if (0 <= newPos && newPos < n) {
if (!best[newPos].count(pwr)) best[newPos][pwr] = 1e16;
if (dist + 1 < best[newPos][pwr]) {
best[newPos][pwr] = dist + 1;
pq.push({-best[newPos][pwr], {newPos, pwr}});
}
}
}
}
ll ans = 1e16;
for (auto x : best[spot[1]]) ans = min(ans, x.second);
cout << (ans != 1e16 ? ans : -1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(val, -1, sizeof(val));
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> spot[i] >> jump[i];
val[spot[i]] = i;
}
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
2176 KB |
Output is correct |
4 |
Correct |
1 ms |
2140 KB |
Output is correct |
5 |
Correct |
1 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2140 KB |
Output is correct |
7 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
2172 KB |
Output is correct |
3 |
Correct |
1 ms |
2140 KB |
Output is correct |
4 |
Correct |
1 ms |
2140 KB |
Output is correct |
5 |
Correct |
1 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2140 KB |
Output is correct |
7 |
Correct |
1 ms |
2140 KB |
Output is correct |
8 |
Correct |
1 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
2140 KB |
Output is correct |
10 |
Correct |
2 ms |
2140 KB |
Output is correct |
11 |
Incorrect |
2 ms |
2392 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
2140 KB |
Output is correct |
4 |
Correct |
1 ms |
2140 KB |
Output is correct |
5 |
Correct |
1 ms |
2140 KB |
Output is correct |
6 |
Correct |
2 ms |
2140 KB |
Output is correct |
7 |
Correct |
1 ms |
2140 KB |
Output is correct |
8 |
Correct |
1 ms |
2136 KB |
Output is correct |
9 |
Correct |
1 ms |
2140 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2140 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
2140 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2136 KB |
Output is correct |
7 |
Correct |
2 ms |
2140 KB |
Output is correct |
8 |
Correct |
1 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
2140 KB |
Output is correct |
10 |
Correct |
2 ms |
2220 KB |
Output is correct |
11 |
Incorrect |
2 ms |
2392 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2140 KB |
Output is correct |
2 |
Correct |
1 ms |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
2148 KB |
Output is correct |
4 |
Correct |
1 ms |
2140 KB |
Output is correct |
5 |
Correct |
1 ms |
2140 KB |
Output is correct |
6 |
Correct |
1 ms |
2140 KB |
Output is correct |
7 |
Correct |
1 ms |
2224 KB |
Output is correct |
8 |
Correct |
1 ms |
2140 KB |
Output is correct |
9 |
Correct |
1 ms |
2140 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Incorrect |
1 ms |
2140 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |