#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef pair<pair<int,int>,int> pp;
int N,M;
int B[30005],P[30005];
vector<int> V[30005];
priority_queue<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
// |
// |
// |
// V
//dist pos ind
Q.push({{0,B[0]},0});
while(!Q.empty()){
pp p=Q.top();
Q.pop();
//cout<<Q.size()<<" "<<p.second<<" "<<p.first.first<<" "<<p.first.second<<endl;
if(p.second==1){
cout<<p.first.first;
return 0;
}
for(int x:V[p.first.second])
Q.push({{p.first.first,B[x]},x});
V[p.first.second].clear();
if(p.first.second==B[p.second]){
for(int k=1;;k++){
if(p.first.second-k*P[p.second]<0)
break;
if(!V[p.first.second-k*P[p.second]].empty()){
Q.push({{p.first.first+k,p.first.second-k*P[p.second]},p.second});
break;
}
}
for(int k=1;;k++){
if(p.first.second+k*P[p.second]>=N)
break;
if(!V[p.first.second+k*P[p.second]].empty()){
Q.push({{p.first.first+k,p.first.second+k*P[p.second]},p.second});
break;
}
}
}
if(p.first.second<B[p.second]){
for(int k=1;;k++){
if(p.first.second-k*P[p.second]<0)
break;
if(!V[p.first.second-k*P[p.second]].empty()){
Q.push({{p.first.first+k,p.first.second-k*P[p.second]},p.second});
break;
}
}
}
if(p.first.second>B[p.second]){
for(int k=1;;k++){
if(p.first.second+k*P[p.second]>=N)
break;
if(!V[p.first.second+k*P[p.second]].empty()){
Q.push({{p.first.first+k,p.first.second+k*P[p.second]},p.second});
break;
}
}
}
}
cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1128 KB |
Output is correct |
3 |
Correct |
3 ms |
1128 KB |
Output is correct |
4 |
Correct |
4 ms |
1204 KB |
Output is correct |
5 |
Correct |
4 ms |
1292 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1364 KB |
Output is correct |
2 |
Correct |
3 ms |
1364 KB |
Output is correct |
3 |
Correct |
4 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
4 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
4 ms |
1364 KB |
Output is correct |
10 |
Incorrect |
5 ms |
1364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1364 KB |
Output is correct |
2 |
Correct |
3 ms |
1364 KB |
Output is correct |
3 |
Correct |
3 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Incorrect |
4 ms |
1364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Correct |
4 ms |
1364 KB |
Output is correct |
3 |
Correct |
4 ms |
1364 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Incorrect |
3 ms |
1364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1364 KB |
Output is correct |
2 |
Correct |
2 ms |
1364 KB |
Output is correct |
3 |
Correct |
3 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1364 KB |
Output is correct |
10 |
Incorrect |
3 ms |
1364 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |