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 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<<30);
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 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];
priority_queue<pll,vector<pll>,greater<pll> > pq;
int main(){
scanf("%d%d",&n,&m);
REP(i,m){
scanf("%d%d",&b[i],&p[i]);
P[p[i]].pb(b[i]);
}
for(ll step=1;step<N;++step){
if(!SZ(P[step]))continue;
vector<vector<ll> > lis;
lis.clear();lis.resize(step);
tmp.clear();
sort(P[step].begin(),P[step].end());
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(ll 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){
if(ii!=x)v[x].pb(mp(ii,(ii-x)/step));
if(ii!=y)v[y].pb(mp(ii,(y-ii)/step));
}
}
for(ll ii=lis[i].back()+step;ii<N;ii+=step)x=lis[i].back(),v[x].pb(mp(ii,(ii-x)/step));
for(ll ii=lis[i][0]-step;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(pll j:v[x])if(dis[j.X]>dis[x]+j.Y)pq.push(mp(dis[j.X]=dis[x]+j.Y,j.X));
}
cout<<(dis[b[1]]==INF?-1:dis[b[1]])<<"\n";
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
skyscraper.cpp:27:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&b[i],&p[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |