#ifdef t9unkubj
#define _GLIBCXX_DEBUG
#include"debug.cpp"
//#include"template_no_debug.h"
#else
#define dbg(...) 199958
#endif
#pragma GCC optimize("O3")
using namespace std;
#include<bits/stdc++.h>
using ll=long long;
using ull=unsigned long long;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
template<class T,class F>
bool chmin(T &x, F y){
if(x>y){
x=y;
return true;
}
return false;
}
template<class T, class F>
bool chmax(T &x, F y){
if(x<y){
x=y;
return true;
}
return false;
}
double pass_time=0;
ll brute(int n,int m,vc<int>b,vc<int>p){
vvc<int>is(n);
rep(i,m){
is[b[i]].push_back(i);
}
vc<int>dp(n,1e9);
using T=pair<int,int>;
priority_queue<T,vc<T>,greater<>>que;
auto update=[&](int a,int b){
if(chmin(dp[a],b))que.push({b,a});
};
update(b[0],0);
while(que.size()){
auto [d,a]=que.top();que.pop();
if(dp[a]!=d)continue;
for(auto&i:is[a]){
for(int t=0;;t++){
if(a+p[i]*t<n)update(a+p[i]*t,t+dp[a]);
else break;
}
for(int t=0;;t++){
if(a-p[i]*t>=0)update(a-p[i]*t,t+dp[a]);
else break;
}
}
}
int ans=dp[b[1]];
if(ans>1e8)return -1;
return ans;
}
void run(){
int n,m;
cin>>n>>m;
vc<int>b(m),p(m);
rep(i,m)cin>>b[i]>>p[i];
cout<<brute(n,m,b,p)<<"\n";
}
signed main(){
#ifdef t9unkubj
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
pass_time=clock();
int t=1;
//cin>>t;
while(t--){
run();
}
pass_time=clock()-pass_time;
dbg(pass_time/CLOCKS_PER_SEC);
}
/*
5 3
0 2
1 1
4 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... |