이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |