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 <int, int> d;
int & get(int x, int y) {
y++;
return d[x * n0 + y];
}
void bfs(int sx, int sy) {
deque <array <int, 2>> q;
get(sx, sy) = 1;
q.pb({sx, sy});
while (!q.empty()) {
auto p = q.front();
q.pop_front();
int & d1 = get(p[0], p[1]);
if (p[1] != -1) {
if (p[0] + p[1] < n) {
int & d2 = get(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 = get(p[0] - p[1], p[1]);
if (!d2 || d2 > d1 + 1) {
d2 = d1 + 1;
q.pb({p[0] - p[1], p[1]});
}
}
int & d2 = get(p[0], -1);
if (!d2 || d2 > d1) {
d2 = d1;
q.push_front({p[0], -1});
}
} else {
for (int i : can[p[0]]) {
int & d2 = get(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);
int & res = get(B, -1);
if (!res) cout << -1;
else cout << res - 1;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:18:13: warning: 'B' may be used uninitialized in this function [-Wmaybe-uninitialized]
18 | return d[x * n0 + y];
| ~~^~~~
skyscraper.cpp:64:9: note: 'B' was declared here
64 | int A, B;
| ^
skyscraper.cpp:72:5: warning: 'A' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | 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... |