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;
vector<pair<int,int> >v[30005];
priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> > >pq;
main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N,M;
cin>>N>>M;
bool occ[N+3];
memset(occ,0,sizeof(occ));
vector<pair<int,int> >hi;
for(int i=0;i<M;i++){
int a,b;
cin>>a>>b;
occ[a] = 1;
hi.push_back(make_pair(a,b));
}
pq.push(make_pair(0,0));
int dist[40005];
for(int i=0;i<35004;i++) dist[i] = 1e9;
dist[0] = 0;
while(!pq.empty()){
int dis = pq.top().first;
int no = pq.top().second;
pq.pop();
if(dist[no]<dis)continue;
if(v[no].empty()){
for(int i=0;i<M;i++){
if(abs(hi[i].first-hi[no].first)%hi[no].second!=0) continue;
int lol = dis+abs(hi[i].first-hi[no].first)/hi[no].second;
if(dist[i]>lol){
v[no].push_back(make_pair(i,lol-dis));
}
}
}
for(pair<int,int>p:v[no]){
if(dis+p.second<dist[p.first]){
dist[p.first] = dis+p.second;
pq.push(make_pair(dis+p.second,p.first));
}
}
}
if(dist[1] == 1e9) cout<<"-1";
else cout<<dist[hi[1].first];
}
Compilation message (stderr)
skyscraper.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
6 | main(){
| ^~~~
# | 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... |