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;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 30003;
int n, m, k;
vector <int> can[n0];
map <array <int, 2>, int> d;
void bfs(int sx, int sy) {
deque <array <int, 2>> q;
d[{sx, sy}] = 1;
q.pb({sx, sy});
while (!q.empty()) {
auto p = q.front();
q.pop_front();
int d1 = d[p];
if (p[1] != -1) {
if (p[0] + p[1] < n) {
int & d2 = d[{p[0] + p[1], p[1]}];
if (!d2 || d2 > d1 + 1) {
d2 = d1 + 1;
q.pb({p[0] + p[1], p[1]});
}
}
if (p[0] - p[1] >= 0) {
int & d2 = d[{p[0] - p[1], p[1]}];
if (!d2 || d2 > d1 + 1) {
d2 = d1 + 1;
q.pb({p[0] - p[1], p[1]});
}
}
int & d2 = d[{p[0], -1}];
if (!d2 || d2 > d1) {
d2 = d1;
q.push_front({p[0], -1});
}
} else {
for (int i : can[p[0]]) {
int & d2 = d[{p[0], i}];
if (!d2 || d2 > d1) {
d2 = d1;
q.push_front({p[0], i});
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n >> m;
int A, B;
for (int i = 0; i < m; i++) {
int b, p;
cin >> b >> p;
if (i == 0) A = b;
if (i == 1) B = b;
can[b].pb(p);
}
bfs(A, -1);
if (!d.count({B, -1})) cout << -1;
else cout << d[{B, -1}] - 1;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:69:24: warning: 'B' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | else cout << d[{B, -1}] - 1;
| ^
skyscraper.cpp:67:5: warning: 'A' may be used uninitialized in this function [-Wmaybe-uninitialized]
67 | bfs(A, -1);
| ~~~^~~~~~~
# | 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... |