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) 1e18 + 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);
vector<map<int,int>> d(n+1);
deque<pair<int,int>> dq;
d0[b0] = 0;
dq.push_back(mp(b0,0));
while(dq.size()) {
int i = dq.front().fr;
int p = dq.front().sc;
dq.pop_front();
if(p == 0) {
for(auto x : dogs[i]) {
if(d[i].count(x) == 0 or d[i][x] > d0[i]) {
d[i][x] = d0[i];
dq.push_front(mp(i,x));
}
}
}
else {
if(d0[i] > d[i][p]) {
d0[i] = d[i][p];
dq.push_front(mp(i,0));
}
int i1;
i1 = i-p;
if(i1 >= 0 && i1 <= n-1 && (d[i1].count(p) == 0 or d[i1][p] > d[i][p]+1)) {
d[i1][p] = d[i][p]+1;
dq.push_back(mp(i1,p));
}
i1 = i+p;
if(i1 >= 0 && i1 <= n-1 && (d[i1].count(p) == 0 or d[i1][p] > d[i][p]+1)) {
d[i1][p] = d[i][p]+1;
dq.push_back(mp(i1,p));
}
}
}
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:68:13: warning: 'b1' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | 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... |