This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int nax = 30005;
const int INF = 1e9 + 5;
vector<int> dog[nax];
vector<int> jump(nax);
ll dp[2005][nax];
int n, m;
ll go(int sky, int doge) {
if (sky < 0 || sky >= n) return INF;
if (doge == 1) return 0;
ll& ret = dp[sky][doge];
if (ret != -1) {
return ret;
}
ret = INF;
// pass news
for(int i : dog[sky]) {
ret = min(ret, go(sky, i));
}
// jump left
if (sky-jump[doge] >= 0) {
ret = min(ret, 1+go(sky-jump[doge], doge));
}
// jump right
if (sky+jump[doge] < n) {
ret = min(ret, 1+go(sky+jump[doge], doge));
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
memset(dp, -1, sizeof dp);
cin >> n >> m;
int boss = 0;
for(int i = 0; i < m; i++) {
int idx, p;
cin >> idx >> p;
dog[idx].push_back(i);
jump[i] = p;
if (i == 0) {
boss = idx;
}
}
int ans = go(boss, 0);
if (ans >= INF) {
cout << -1 << "\n";
} else {
cout << ans << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |