#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
typedef pair<int,pair<int,int>> pp;
int N,M;
int B[30005],P[30005];
vector<int> V[30005];
deque<pp> Q;
int main(){
cin>>N>>M;
for(int i=0;i<M;i++){
cin>>B[i]>>P[i];
if(i)
V[B[i]].push_back(i);
}
//indice distanza posizione
Q.push_back({0,{0,B[0]}});
while(!Q.empty()){
pp p=Q.front();
Q.pop_front();
//cout<<Q.size()<<" "<<p.first<<" "<<p.second.first<<" "<<p.second.second<<endl;
if(p.first==1){
cout<<p.second.first;
return 0;
}
for(int x:V[p.second.second])
Q.push_front({x,{p.second.first,B[x]}});
V[p.second.second].clear();
if(p.second.second==B[p.first]){
for(int k=1;;k++){
if(p.second.second-k*P[p.first]<0)
break;
if(!V[p.second.second-k*P[p.first]].empty()){
Q.push_back({p.first,{p.second.first+k,p.second.second-k*P[p.first]}});
break;
}
}
for(int k=1;;k++){
if(p.second.second+k*P[p.first]>=N)
break;
if(!V[p.second.second+k*P[p.first]].empty()){
Q.push_back({p.first,{p.second.first+k,p.second.second+k*P[p.first]}});
break;
}
}
}
if(p.second.second<B[p.first]){
for(int k=1;;k++){
if(p.second.second-k*P[p.first]<0)
break;
if(!V[p.second.second-k*P[p.first]].empty()){
Q.push_back({p.first,{p.second.first+k,p.second.second-k*P[p.first]}});
break;
}
}
}
if(p.second.second>B[p.first]){
for(int k=1;;k++){
if(p.second.second+k*P[p.first]>=N)
break;
if(!V[p.second.second+k*P[p.first]].empty()){
Q.push_back({p.first,{p.second.first+k,p.second.second+k*P[p.first]}});
break;
}
}
}
}
cout<<-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
964 KB |
Output is correct |
2 |
Correct |
3 ms |
1160 KB |
Output is correct |
3 |
Correct |
3 ms |
1228 KB |
Output is correct |
4 |
Correct |
3 ms |
1236 KB |
Output is correct |
5 |
Correct |
3 ms |
1236 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
3 ms |
1304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1304 KB |
Output is correct |
2 |
Correct |
2 ms |
1304 KB |
Output is correct |
3 |
Correct |
3 ms |
1304 KB |
Output is correct |
4 |
Correct |
3 ms |
1304 KB |
Output is correct |
5 |
Correct |
3 ms |
1304 KB |
Output is correct |
6 |
Correct |
3 ms |
1304 KB |
Output is correct |
7 |
Correct |
3 ms |
1304 KB |
Output is correct |
8 |
Correct |
3 ms |
1352 KB |
Output is correct |
9 |
Incorrect |
2 ms |
1352 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1352 KB |
Output is correct |
2 |
Correct |
2 ms |
1352 KB |
Output is correct |
3 |
Correct |
2 ms |
1352 KB |
Output is correct |
4 |
Correct |
3 ms |
1352 KB |
Output is correct |
5 |
Correct |
3 ms |
1352 KB |
Output is correct |
6 |
Correct |
2 ms |
1352 KB |
Output is correct |
7 |
Correct |
2 ms |
1352 KB |
Output is correct |
8 |
Correct |
3 ms |
1352 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1368 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1368 KB |
Output is correct |
2 |
Correct |
3 ms |
1368 KB |
Output is correct |
3 |
Correct |
3 ms |
1384 KB |
Output is correct |
4 |
Correct |
4 ms |
1384 KB |
Output is correct |
5 |
Correct |
2 ms |
1400 KB |
Output is correct |
6 |
Correct |
2 ms |
1400 KB |
Output is correct |
7 |
Correct |
3 ms |
1400 KB |
Output is correct |
8 |
Correct |
3 ms |
1400 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1400 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1400 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1400 KB |
Output is correct |
4 |
Correct |
3 ms |
1400 KB |
Output is correct |
5 |
Correct |
2 ms |
1400 KB |
Output is correct |
6 |
Correct |
3 ms |
1400 KB |
Output is correct |
7 |
Correct |
3 ms |
1400 KB |
Output is correct |
8 |
Correct |
2 ms |
1400 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1400 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |