이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int maxn=3e4+5;
int n,m;
int b[maxn],p[maxn];
vector<int>v[maxn];
unordered_map<int,int>dis;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int mx = max(n,m) + 1;
for(int i = 0;i < m;i++){
cin >> b[i] >> p[i];
v[b[i]].push_back(p[i]);
}
queue<pair<int,int>>q;
q.push({b[0],p[0]});
dis[b[0] * mx + p[0]] = 0;
while(!q.empty()){
int idx = q.front().first;
int pi = q.front().second;
q.pop();
int now = dis[idx * mx + pi];
if(idx == b[1]) return cout<<now,0;
if(idx - pi >= 0){
auto t = dis.find((idx - pi) * mx + pi);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx - pi) * mx + pi] = now + 1;
q.push({idx - pi,pi});
}
}
if(idx + pi < n){
auto t = dis.find((idx + pi) * mx + pi);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx + pi) * mx + pi] = now + 1;
q.push({idx + pi,pi});
}
}
for(auto i : v[idx]){
if(idx - i >= 0){
auto t = dis.find((idx - i) * mx + i);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx - i) * mx + i] = now + 1;
q.push({idx - i,i});
}
}
if(idx + i < n){
auto t = dis.find((idx + i) * mx + i);
if(t == dis.end() or (t != dis.end() and (*t).second > now + 1)){
dis[(idx + i) * mx + i] = now + 1;
q.push({idx + i,i});
}
}
}
}
cout<<-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... |