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;
#define int long long
vector<pair<int,int> >v[30005];
main(){
	int N,M;
	cin>>N>>M;
	clock_t st = clock();
	vector<pair<int,int> >hi;
	for(int i=0;i<M;i++){
		int a,b;
		cin>>a>>b;
		hi.push_back(make_pair(a,b));
	}
	for(int i=0;i<M;i++){
		for(int j=0;j<M;j++){
			if(i==j)continue;
			if(abs(hi[i].first-hi[j].first)%hi[i].second == 0) v[i].push_back(make_pair(j,abs(hi[i].first-hi[j].first)/hi[i].second));
			if(abs(hi[i].first-hi[j].first)%hi[j].second == 0) v[j].push_back(make_pair(i,abs(hi[i].first-hi[j].first)/hi[j].second));
		}
	}
	priority_queue<pair<int,int>,vector<pair<int,int> >, greater<pair<int,int> >  >pq;
	pq.push(make_pair(0,0));
	int dist[30005];
	for(int i=0;i<30004;i++) dist[i] = 1e12;
	dist[0] = 0;
	while(!pq.empty()){
		if((float)(clock()-st)/CLOCKS_PER_SEC>=0.5) break;
		int dis = pq.top().first;
		int no = pq.top().second;
		pq.pop();
		if(dist[no]<dis)continue;
		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] == 1e12) cout<<"-1";
	else cout<<dist[1];
}
Compilation message (stderr)
skyscraper.cpp:5:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    5 | 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... |