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 <iostream>
#include <bitset>
#include <deque>
#include <vector>
using namespace std;
const int MAXN = 2001;
bitset<MAXN> used[MAXN];
bitset<MAXN> used_sky;
vector<int> dog[MAXN];
signed main() {
int n, m;
cin >> n >> m;
deque<pair<pair<int, int>, int>> q;
int togo = 0;
for (int i = 0; i < m; ++i) {
int j, p;
cin >> j >> p;
dog[j].push_back(p);
if (i == 0) {
q.push_back({{j, p}, 0});
used[j][p] = 1;
} else if (i == 1) {
togo = j;
}
}
while (q.size()) {
auto [pr, d] = q.front();
auto [j, p] = pr;
q.pop_front();
if (j == togo) {
cout << d << endl;
return 0;
}
if (!used_sky[j]) {
used_sky[j] = 1;
for (auto p0 : dog[j]) {
if (!used[j][p0]) {
used[j][p0] = 1;
q.push_front({{j, p0}, d});
}
}
}
if (j >= p && !used[j - p][p]) {
used[j - p][p] = 1;
q.push_back({{j - p, p}, d + 1});
}
if (j + p < n && !used[j + p][p]) {
used[j + p][p] = 1;
q.push_back({{j + p, p}, d + 1});
}
}
cout << -1 << endl;
}
# | 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... |