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;
typedef long long ll;
const int MAXN = 3e5 + 2;
int n, m, b[MAXN], p[MAXN], dis[MAXN];
vector< int > adj[MAXN];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i ++){
cin >> b[i] >> p[i];
adj[b[i]].push_back(p[i]);
}
fill(dis, dis + n, INT_MAX/2);
dis[b[0]] = 0;
priority_queue< pair< int, int> > q;
q.push({0, b[0]});
while( q.size() ){
int v = q.top().second;
q.pop();
if(dis[v] == INT_MAX) break;
for(int i = 0; i < adj[v].size(); i ++){
int u = adj[v][i];
int x = 1;
for(int i = v + u; i < n; i += u){
if( dis[i] <= dis[v] + x){
x ++;
continue;
}
dis[i] = dis[v] + x;
if( i != b[1]) q.push({-dis[i], i});
x ++;
}
x = 1;
for(int i = v - u; i >= 0; i -= u){
if( dis[i] <= dis[v] + x){
x ++;
continue;
}
dis[i] = dis[v] + x;
if( i != b[1]) q.push({-dis[i], i});
x ++;
}
}
}
if( dis[b[1]] != INT_MAX) return printf("%d", dis[b[1]]), 0;
printf("%d",-1);
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:24:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < adj[v].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... |