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];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
int main(){
memset(dist,9,sizeof dist);
int n,m;
cin>>n>>m;
int a,b;cin>>a>>b;
doge[a].insert(b);
dist[a]=0;
pq.push({0,a});
for(int i=1;i<m;i++){
cin>>a>>b;
doge[a].insert(b);
}
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[1]!=dist[30099]?dist[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... |