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;
#define rep(i,a,b) for(int i = a; i<int(b);++i)
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define trav(a,c) for(auto a: c)
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
int main(){
cin.sync_with_stdio(false);
ll n,m;
cin>>n>>m;
vi b(m);
vi p(m);
rep(i,0,m) cin>>b[i]>>p[i];
queue<tuple<ll,ll> > q; //(doge,jumps);
q.emplace(0,0);
vector<bool> seen(m);
while(q.size()){
ll doge,jumps;
tie(doge,jumps) = q.front();
q.pop();
if(seen[doge]) continue;
seen[doge] = true;
if(doge==1){
cout<<jumps<<endl;
return 0;
}
rep(i,0,m){
if((b[i]-b[doge])%p[doge]==0)
q.emplace(i,jumps+abs(b[i]-b[doge])/p[doge]);
}
}
cout<<-1<<endl;
}
# | 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... |