#include<bits/stdc++.h>
using namespace std;
const int nax = 3e4 + 3;
const int INF = 1e9;
int b[nax], p[nax];
vector<int>doges[nax];
unordered_map<long long,int>dist;
int get(int x, int y) {
long long H = x * 100000LL + y;
if(dist.find(H)!=dist.end()) return dist[H];
return INF;
}
void put(int x, int y, int val) {
long long H = x * 100000LL + y;
dist[H] = val;
}
void PlayGround() {
int n, m;
cin>>n>>m;
for(int i=0; i<m; ++i) {
cin>>b[i]>>p[i];
doges[b[i]].push_back(p[i]);
}
put(b[0], 0, 0);
queue<tuple<int,int,int>>q;
q.push({b[0], 0, 0});
while(!q.empty()) {
auto x = q.front();
q.pop();
int ss = get<0>(x);
int j = get<1>(x);
int d = get<2>(x);
if(ss==b[1]) {
cout<<d<<'\n';
return;
}
if(d!=get(ss, j)) continue;
if(j==0) {
for(int z : doges[ss]) {
if(get(ss, z)>d) {
put(ss, z, d);
q.emplace(ss, z, d);
}
}
} else {
if(ss+j<n) {
if(get(ss+j, j)>d+1) {
put(ss+j, j, d+1);
q.emplace(ss+j, j, d+1);
}
if(get(ss+j, 0)>d+1) {
put(ss+j, 0, d+1);
q.emplace(ss+j, 0, d+1);
}
}
if(ss-j>=0) {
if(get(ss-j, j)>d+1) {
put(ss-j, j, d+1);
q.emplace(ss-j, j, d+1);
}
if(get(ss-j, 0)>d+1) {
put(ss-j, 0, d+1);
q.emplace(ss-j, 0, d+1);
}
}
}
}
cout<<"-1\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |