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
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3e4 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, m;
int b[N], p[N];
bool vis[N][205];
int mn_dist[N];
vector<int> doges[N];
priority_queue<pair<int, ii>, vector<pair<int, ii>>, greater<pair<int, ii>>> pq;
bool beg[N];
void process(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
b[i]++;
doges[b[i]].pb(p[i]);
}
for(int i = 1; i <= n; i++) mn_dist[i] = oo;
pq.push({0, {b[1], p[1]}});
mn_dist[b[1]] = 0;
if(p[1] <= 200) vis[b[1]][p[1]] = 1;
while(!pq.empty()){
int d = pq.top().fi, u = pq.top().se.fi, p = pq.top().se.se;
pq.pop();
if(p <= 200){
if((u > p) && !vis[u - p][p]){
vis[u - p][p] = 1;
mn_dist[u - p] = min(mn_dist[u - p], d + 1);
pq.push({d + 1, {u - p, p}});
}
if((u + p) <= n && !vis[u + p][p]){
vis[u + p][p] = 1;
mn_dist[u + p] = min(mn_dist[u + p], d + 1);
pq.push({d + 1, {u + p, p}});
}
}
if(d == mn_dist[u] && !beg[u]){
beg[u] = 1;
for(auto it : doges[u]){
if(it <= 200){
vis[u][it] = 1;
pq.push({d, {u, it}});
}
else{
for(int i = (u % it); i <= n; i += it){
if(!i) continue;
int dist = abs(i - u) / it;
if(mn_dist[i] > (mn_dist[u] + dist)){
mn_dist[i] = mn_dist[u] + dist;
pq.push({mn_dist[i], {i, it}});
}
}
}
}
}
}
cout << (mn_dist[b[2]] == oo ? -1 : mn_dist[b[2]]) << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
//freopen("doge.inp", "r", stdin);
//freopen("doge.out", "w", stdout);
cin.tie(0);
process();
}
# | 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... |