#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=3e4+5;
const ll INF=(1LL<<60);
const ld pi=3.14159265359;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
ll n,m,b[N],p[N],x,y,dis[N];
vector<ll> P[N],tmp;
vector<pll> v[N];
vector<vector<ll> > lis;
priority_queue<pll,vector<pll>,greater<pll> > pq;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP(i,m){
cin>>b[i]>>p[i];
P[p[i]].pb(b[i]);
}
for(ll step=1;step<N;++step){
if(!SZ(P[step]))continue;
lis.clear();lis.resize(step);
tmp.clear();
for(auto i:P[step]){
lis[i%step].pb(i);
tmp.pb(i%step);
}
sort(tmp.begin(),tmp.end());
tmp.resize(unique(tmp.begin(),tmp.end())-tmp.begin());
for(auto i:tmp){
sort(lis[i].begin(),lis[i].end());
for(ll j=1;j<SZ(lis[i]);++j){
x=lis[i][j-1],y=lis[i][j];
for(ll ii=x;ii<=y;ii+=step){
v[x].pb(mp(ii,(ii-x)/step));
v[y].pb(mp(ii,(y-ii)/step));
}
}
for(ll ii=lis[i].back();ii<N;ii+=step)x=lis[i].back(),v[x].pb(mp(ii,(ii-x)/step));
for(ll ii=lis[i][0];ii>=0;ii-=step)x=lis[i][0],v[x].pb(mp(ii,(x-ii)/step));
}
}
REP(i,N)dis[i]=INF;
pq.push(mp(dis[b[0]]=0,b[0]));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(dis[x]!=y)continue;
for(auto j:v[x])if(dis[j.X]>dis[x]+j.Y)dis[j.X]=dis[x]+j.Y,pq.push(mp(dis[j.X],j.X));
}
cout<<(dis[b[1]]==INF?-1:dis[b[1]])<<"\n";
return 0;
}
Compilation message
skyscraper.cpp:8:18: warning: overflow in implicit constant conversion [-Woverflow]
const ll INF=(1LL<<60);
~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
2292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
2292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |