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;
const long long inf = (long long) 1e9 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 3e4+10;
int n, m;
vector<int> dogs[maxn];
void solve() {
cin >> n >> m;
int b0,b1;
for(int i = 0; i < m; i++) {
int b,p; cin >> b >> p;
if(i == 0) b0 = b;
if(i == 1) b1 = b;
dogs[b].pb(p);
}
vector<int> d0(n+1,inf), mark0(n+1,0);
vector<map<int,int>> d(n+1);
queue<pair<int,int>> q;
d0[b0] = 0;
q.push(mp(b0,0));
while(q.size()) {
int i = q.front().fr;
int p = q.front().sc;
q.pop();
if(p == 0) {
if(mark0[i]) continue;
mark0[i] = 1;
}
if(p == 0) {
for(auto x : dogs[i]) {
if(i-x >= 0) {
if(d[i-x].count(x) == 0 or d[i-x][x] > d0[i]+1) {
d[i-x][x] = d0[i]+1;
q.push(mp(i-x,x));
}
if(d0[i-x] > d0[i]+1) {
d0[i-x] = d0[i]+1;
q.push(mp(i-x,0));
}
}
if(i+x <= n-1) {
if(d[i+x].count(x) == 0 or d[i+x][x] > d0[i]+1) {
d[i+x][x] = d0[i]+1;
q.push(mp(i+x,x));
}
if(d0[i+x] > d0[i]+1) {
d0[i+x] = d0[i]+1;
q.push(mp(i+x,0));
}
}
}
}
else {
if(i-p >= 0) {
if(d[i-p].count(p) == 0 or d[i-p][p] > d[i][p]+1) {
d[i-p][p] = d[i][p]+1;
q.push(mp(i-p,p));
}
if(d0[i-p] > d[i][p]+1) {
d0[i-p] = d[i][p]+1;
q.push(mp(i-p,0));
}
}
if(i+p <= n-1) {
if(d[i+p].count(p) == 0 or d[i+p][p] > d[i][p]+1) {
d[i+p][p] = d[i][p]+1;
q.push(mp(i+p,p));
}
if(d0[i+p] > d[i][p]+1) {
d0[i+p] = d[i][p]+1;
q.push(mp(i+p,0));
}
}
}
}
if(d0[b1] == inf) cout << -1 << endl;
else cout << d0[b1] << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Compilation message (stderr)
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:34:19: warning: 'b0' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | q.push(mp(b0,0));
| ^
skyscraper.cpp:95:13: warning: 'b1' may be used uninitialized in this function [-Wmaybe-uninitialized]
95 | if(d0[b1] == inf) 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... |