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);
vector<vi > doges(n);
vector<vi> e;
rep(i,0,m) {
cin>>b[i]>>p[i];
doges[b[i]].push_back(i);
}
priority_queue<pair<ll,ll>,vector<pii>,greater<pii> > q; //(jumps,doge);
q.emplace(0,b[0]);
vector<bool> seen(n);
while(q.size()){
ll pos,jumps;
tie(jumps,pos) = q.top();
q.pop();
//cout<<pos<<" "<<jumps<<": "<<endl;
if(seen[pos]) continue;
seen[pos] = true;
rep(j,0,doges[pos].size())
if(doges[pos][j]==1){
cout<<jumps<<endl;
_Exit(0);
}
rep(i,0,n){
if(seen[i]) continue;
ll mn = 1e18;
rep(j,0,doges[pos].size()){
if((i-pos)%p[doges[pos][j]]==0) mn = min(mn,abs(i-pos)/p[doges[pos][j]]);
}
if(mn<1e18)
q.emplace(mn+jumps,i);
}
/*
vector<bool> seenHere(n);
trav(doge,doges[pos]){
if(doge==1){
cout<<jumps<<endl;
return 0;
}
//rep(i,0,n)
// if((i-pos)%p[doge]==0&&!seen[i])
// q.emplace(jumps+abs(i-pos)/p[doge],i);
ll pi = p[doge];
rep(i,1,n/pi+1){
//cout<<pos+pi*i<<endl;
if(pos+pi*i<n) {
//if(seenHere[pos+pi*i]) break;
//seenHere[pos+pi*i] = true;
q.emplace(jumps+i,pos+pi*i);
}
if(pos-pi*i>=0) {
//if(seenHere[pos-pi*i]) break;
//seenHere[pos-pi*i] = true;
q.emplace(jumps+i,pos-pi*i);
}
}
}
/*rep(i,0,n){
if((b[i]-b[doge])%p[doge]==0&&!seen[i])
q.emplace(jumps+abs(b[i]-b[doge])/p[doge],i);
}*/
}
cout<<-1<<endl;
}
Compilation message (stderr)
skyscraper.cpp:88:9: warning: "/*" within comment [-Wcomment]
/*rep(i,0,n){
# | 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... |