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;
const int N=3e4+2;
const int magic=180;
const int inf=1e9+7;
const int LIM=N*magic+N;
int b[N],p[N],dis[LIM];
vector<pair<int,int> > adj[LIM];
priority_queue<pair<int,int> > lis;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,i,j,k,l;
cin>>n>>m;
for(i=0;i<m;i++){ //0->m-1
cin>>b[i]>>p[i];
}
//m->m+n-1 la cac nut dai dien
//m+n -> m+n*sqrt(n) la cac nut dai dien cho cac luong van tai
for(i=1;i<=min(magic,n-1);i++){
for(j=0;j<n-i;j++){
k=i*n+j+m;
l=i*n+j+i+m;
adj[k].push_back({l,1});
adj[l].push_back({k,1});
}
for(j=0;j<n;j++){
adj[m+j].push_back({i*n+j+m,0});
}
}
for(i=0;i<m;i++){
if(p[i]>magic){
j=b[i];
k=0;
while(j>=0){
adj[i].push_back({m+j,k});
j-=p[i];
k++;
}
j=b[i];
while(true){
j+=p[i];
k++;
if(j>=n){
break;
}
adj[i].push_back({m+j,k});
}
}
else{
for(j=1;j<=min(n,magic);j++){
k=j*n+b[i]+m;
adj[k].push_back({i,0});
}
k=p[i]*n+b[i]+m;
adj[i].push_back({k,0});
}
}
for(i=1;i<=m+magic*n;i++){
dis[i]=inf;
}
dis[0]=0;
lis.push({0,0});
while(lis.size()){
j=-lis.top().first;
k=lis.top().second;
lis.pop();
if(dis[k]==j){
// cout<<k<<endl;
for(i=0;i<adj[k].size();i++){
// cout<<adj[k][i].first<<endl;
if(dis[adj[k][i].first]>dis[k]+adj[k][i].second){
dis[adj[k][i].first]=dis[k]+adj[k][i].second;
lis.push({-dis[adj[k][i].first],adj[k][i].first});
}
}
}
}
if(dis[1]!=inf){
cout<<dis[1];
}
else{
cout<<-1;
}
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:70:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<adj[k].size();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... |