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<bits/stdc++.h>
using namespace std;
set<int>doge[30100];
int dist[30100],pos[30100];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
int main(){
memset(dist,9,sizeof dist);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
int b;cin>>pos[i]>>b;
doge[pos[i]].insert(b);
}
dist[pos[0]]=0;
pq.push({0,pos[0]});
while(pq.size()){
auto[a,b]=pq.top();
pq.pop();
if(dist[b]!=a)continue;
for(auto d:doge[b]){
for(int i=b-d;i>=0;i-=d){
int D=a+(b-i)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
for(int i=b+d;i<n;i+=d){
int D=a+(i-b)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
}
}
cout<<(dist[pos[1]]!=dist[30099]?dist[pos[1]]:-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... |