# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163025 | giorgikob | 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>
#define ll long long
using namespace std;
const int N = 3e4 + 69;
int n,m;
vector<int>V[N];
vector< pair<int,int> >gr[N];
int X[N],P[N],D[N],fix[N];
priority_queue< pair<int,int> > s;
int main(){
ios::sync_with_stdio(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>X[i]>>P[i];
X[i]++;
V[X[i]].push_back(i);
}
for(int i=1;i<=m;i++){
if(i>1){
D[i] = 1e9;
//s.insert({1e9,i});
}
}
D[1] = 0;
s.push({0,1});
while(!s.empty()){
pair<int,int> p = s.top();
s.pop();
if(fix[i])continue;
int i = p.second;
fix[i] = 1;
int dist = p.first;
if(i==2){
if(D[2]==1e9)D[2]=-1;
cout<<D[2]<<endl;
return 0;
}
for(int j=X[i];j<=n;j+=P[i]){
for(int k=0;k<V[j].size();k++){
int to = V[j][k];
if(D[to] > dist + (j-X[i])/P[i]){
D[to] = dist + (j-X[i])/P[i];
s.push({D[to],to});
}
}
}
for(int j=X[i]-P[i];j>=1;j-=P[i]){
for(int k=0;k<V[j].size();k++){
int to = V[j][k];
if(D[to] > dist + (X[i]-j)/P[i] ){
D[to] = dist + (X[i]-j)/P[i];
s.push({D[to],to});
}
}
}
}
if(D[2]==1e9)
D[2] = -1;
cout<<D[2]<<endl;
}