#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;
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]]] = 1e18;
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] = 1e18;
if (dist + 1 < best[newPos][pwr]) {
best[newPos][pwr] = dist + 1;
pq.push({-best[newPos][pwr], {newPos, pwr}});
}
}
}
}
ll ans = 1e18;
for (auto x : best[spot[1]]) ans = min(ans, x.second);
cout << (ans != 1e18 ? 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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
8 |
Correct |
2 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1948 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
2140 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1988 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
2 ms |
1880 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
2 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1884 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1880 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Incorrect |
2 ms |
1884 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |