# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
208698 | YJU | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 0 ms | 0 KiB |
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;
typedef long long 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,vis[N],dis[N],b[N],p[N],ti,x;
vector<ll> v[N];
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];
v[b[i]].pb(p[i]);
}
REP(i,n)sort(v[i].rbegin(),v[i].rend()),v[i].resize(unique(v[i].begin(),v[i].end())-v[i].begin());
REP(i,N)dis[i]=INF;
pq.push(mp(0,b[0]));
while(SZ(pq)){
dis[x=pq.top().Y]=pq.top().X;pq.pop();
++ti;
for(ll step:v[x]){
for(ll i=x+step,tt=1;i<n;i+=step,++tt){
if(vis[i]==ti){continue;}
vis[i]=ti;
if(dis[i]>dis[x]+tt)pq.push(mp(dis[x]+tt,i));
}
for(ll i=x-step,tt=1;i>=0;i-=step,++tt){
if(vis[i]==ti)continue;
vis[i]=ti;
if(dis[i]>dis[x]+tt)pq.push(mp(dis[x]+tt,i));
}
}
}
cout<<(dis[b[1]]>=INF?-1:dis[b[1]])<<"\n";
return 0;
}