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>
#define int long long
#define fi first
#define se second
#define keish ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
const int mod = 1e9 + 7;
int n, m;
signed main(){
keish;
cin >> n >> m;
vector<int> b(m), p(m);
vector<vector<int>> e(n);
for(int i = 0; i < n; i++){
cin >> b[i] >> p[i];
e[b[i]].push_back(p[i]);
}
vector<vector<int>> dis(n, vector<int>(2005));
queue<pair<int, int>> q;
for(auto x : e[b[0]]){
dis[b[0]][x] = 0;
q.push({b[0], x});
}
while(!q.empty()){
auto [u, d] = q.front(); q.pop();
if(u == b[1]){
cout << dis[u][d] << '\n';
return 0;
}
if(u + d < n){
if(dis[u + d][d] != -1){
dis[u + d][d] = dis[u][d] + 1;
q.push({u + d, d});
}
for(auto v : e[u + d]){
if(dis[u + d][v] != -1){
dis[u + d][v] = dis[u][d] + 1;
q.push({u + d, v});
}
}
}
if(u - d >= 0){
if(dis[u - d][d] != -1){
dis[u - d][d] = dis[u][d] + 1;
q.push({u - d, d});
}
for(auto v : e[u - d]){
if(dis[u - d][v] != -1){
dis[u - d][v] = dis[u][d] + 1;
q.push({u - d, v});
}
}
}
}
cout << -1 << '\n';
}
# | 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... |