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 pb push_back
#define pii pair<int,int>
const int maxn = 3e4 + 5, INF = 0x3f3f3f3f;
vector<int> doges[maxn];
int dist[maxn];
priority_queue<pii, vector<pii>, greater<pii>> pq;
int main(){
int n, m; cin >> n >> m;
for(int i = 0; i < m; i++){
int bi, pi; cin >> bi >> pi;
doges[bi].pb(pi);
}
for(int i = 0; i < n; i++) dist[i] = INF;
pq.push({0,0});
dist[0] = 0;
while(pq.size()){
auto[d,u] = pq.top(); pq.pop();
if(dist[u]<d) continue;
if(u==1) break;
for(int x : doges[u]){
for(int i = u+x; i < n; i+=x){
int wv = (i-u)/x;
if(dist[i] > dist[u]+wv)
dist[i] = dist[u]+wv, pq.push({dist[i], i});
}
for(int i = u-x; i >= 0; i-=x){
int wv = (u-i)/x;
if(dist[i] > dist[u]+wv)
dist[i] = dist[u]+wv, pq.push({dist[i], i});
}
}
}
if(dist[1] == INF) cout << -1;
else cout << dist[1];
cout << '\n';
}
# | 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... |