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 pb push_back
#define int long long
#define mp make_pair
using namespace std;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int N = 3e4 + 5;
vector<int> adj[N];
int32_t main(){
ios_base::sync_with_stdio(false), cin.tie();
int n, m;
cin>>n>>m;
int start = 0, finish = 0;
for(int i = 0; i < m; i++){
int b, p;
cin>>b>>p;
if(i == 0) start = b;
if(i == 1) finish = b;
adj[b].pb(p);
}
priority_queue<pair<int, int> > pq;
vector<int> best(N, inf), vis(N);
best[start] = 0;
pq.push(mp(0, start));
while(pq.size()){
int node = pq.top().second;
int len = pq.top().first * (-1);
pq.pop();
if(vis[node]) continue;
vis[node] = 1;
for(auto itr: adj[node]){
int dis = 1;
for(int i = node + itr; i < n; i += itr){
if(best[i] > best[node] + dis){
pq.push(mp(-(best[node] + dis), i));
best[i] = best[node] + dis;
}
dis++;
}
dis = 1;
for(int i = node - itr; i >= 0; i -= itr){
if(best[i] > best[node] + dis){
pq.push(mp(-(best[node] + dis), i));
best[i] = best[node] + dis;
}
dis++;
}
}
}
if(best[finish] == inf) cout<<-1<<endl;
else cout<<best[finish]<<endl;
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:34:13: warning: unused variable 'len' [-Wunused-variable]
34 | int len = pq.top().first * (-1);
| ^~~
# | 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... |