This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
cin.tie(NULL);
ios::sync_with_stdio(false);
int N,M,pos,S;
cin>>N>>M;
vector<pair<int,int> > doge(M);
vector<int> distancia(N,1e9);
for(int i=0;i<M;i++){
cin>>pos>>S;
doge[i]=make_pair(pos,S);
}
queue <pair<pair<int,int>,int> > q;
q.push(make_pair(make_pair(doge[0].first,doge[0].second),0));
distancia[0]=0;
while(!q.empty()){
int p=q.front().first.first;
int mov=q.front().first.second;
int jump=q.front().second;
q.pop();
int po=p;
int mo=mov;
int jum=jump;
while(p){
p-=mov;
jump++;
if(p<0)break;
if(distancia[p]>jump){
distancia[p]=jump;
//cout<<distancia[p]<<' '<<p<<' ';
for(int i=0;i<M;i++)if(doge[i].first==p&&p!=1)q.push(make_pair(doge[i],jump));
}
}
while(po<N){
jum++;
po+=mo;
if(po>=N)break;
if(distancia[po]>jum){
distancia[po]=jum;
//cout<<distancia[po]<<' '<<po<<' ';
for(int i=0;i<M;i++)if(doge[i].first==po&&po!=1)q.push(make_pair(doge[i],jum));
}
}
}
if(distancia[1]==1e9)cout<<-1;
else cout<<distancia[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... |