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 <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
const int MXN = 30001, INF = 1e9 + 5;
void solve() {
int N, M;
cin >> N >> M;
int sq = 1;
while (sq * sq < N)
sq++;
vector<int> powers[N];
int location[M];
vector<vector<int>> has(N, vector<int>(sq + 1, 0));
for (int i = 0; i < M; i++) {
int B, P;
cin >> B >> P;
location[i] = B;
powers[B].push_back(P);
if (P <= sq)
has[B][P] = 1;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
vector<int> dist(N + 1, INF);
dist[location[0]] = 0;
pq.emplace(0, location[0]);
while (!pq.empty()) {
auto [cur_dist, u] = pq.top();
pq.pop();
if (dist[u] != cur_dist) continue;
for (const auto &p : powers[u]) {
for (int v = u + p, jumps = 1; v < N; v += p, jumps++) {
int new_dist = cur_dist + jumps;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
}
if (p <= sq && has[v][p]) break;
}
for (int v = u - p, jumps = 1; v >= 0; v -= p, jumps++) {
int new_dist = cur_dist + jumps;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
}
if (p <= sq && has[v][p]) break;
}
}
}
cout << (dist[location[1]] < INF ? dist[location[1]] : -1);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
# | 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... |