#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld double
#define ll long long
mt19937 rnd(51);
const int N = 30010, K = 200, INF = 1e9;
int d[N][K];
bool used[N];
deque<pair<int,int>> arr[2 * N];
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < K; j++) {
d[i][j] = INF;
}
}
int n, m;
cin >> n >> m;
vector<int> b(m), p(m), ans(n, INF);
vector<vector<int>> need(n);
for (int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
need[b[i]].pb(p[i]);
}
ans[b[0]] = 0;
arr[0].pb({b[0], -1});
for (int i = 0; i < N; i++) {
while (arr[i].size() > 0) {
int v = arr[i][0].first, j = arr[i][0].second;
arr[i].pop_front();
if (j == -1) {
if (ans[v] != i) continue;
} else if (d[v][j] != i) {
continue;
}
if (j == -1) {
for (auto to : need[v]) {
if (to < K) {
if (d[v][to] > ans[v]) {
d[v][to] = ans[v];
arr[i].pb({v, to});
}
} else {
for (int j = v, add = 0; j < n; j += to, add++) {
if (ans[j] > ans[v] + add) {
ans[j] = ans[v] + add;
arr[ans[j]].pb({j, -1});
}
}
for (int j = v, add = 0; j >= 0; j -= to, add--) {
if (ans[j] > ans[v] + add) {
ans[j] = ans[v] + add;
arr[ans[j]].pb({j, -1});
}
}
}
}
} else {
if (d[v][j] < ans[v]) {
ans[v] = d[v][j];
arr[i].pb({v, -1});
}
if (v + j < n && d[v + j][j] > d[v][j] + 1) {
d[v + j][j] = d[v][j] + 1;
arr[d[v + j][j]].pb({v + j, j});
}
if (v - j >= 0 && d[v - j][j] > d[v][j] + 1) {
d[v - j][j] = d[v][j] + 1;
arr[d[v - j][j]].pb({v - j, j});
}
}
}
}
cout << (ans[b[1]] == INF ? -1 : ans[b[1]]) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
64092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
64212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
64108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
64212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
38 ms |
64076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |