#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int maxn = 3e4 + 5;
const int S = 174;
int n, m, b[maxn], p[maxn], dist[maxn][S + 1];
vector<pair<int, pair<int, int>>> adj[maxn][S + 1];
vector<int> pwAt[maxn];
void solve() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
pwAt[b[i]].push_back(p[i]);
}
for (int i = 0; i < maxn; ++i)
for (int j = 0; j <= S; ++j)
dist[i][j] = INF;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push(make_pair(0, make_pair(b[0], 0)));
dist[b[0]][0] = 0;
if (p[0] <= S) {
pq.push(make_pair(0, make_pair(b[0], p[0])));
dist[b[0]][p[0]] = 0;
}
while (!pq.empty()) {
auto top = pq.top();
pq.pop();
int w = top.F, u = top.S.F, v = top.S.S;
if (dist[u][v] != w) continue;
if (v == 0) {
for (int power: pwAt[u]) {
if (power <= S) {
if (minimize(dist[u][power], w))
pq.push(make_pair(w, make_pair(u, power)));
} else {
for (int i = u + power; i < n; i += power) {
int c = (i - u) / power;
if (minimize(dist[i][0], w + c))
pq.push(make_pair(w + c, make_pair(i, 0)));
}
for (int i = u - power; i >= 0; i -= power) {
int c = (u - i) / power;
if (minimize(dist[i][0], w + c))
pq.push(make_pair(w + c, make_pair(i, 0)));
}
}
}
} else {
if (u + v < n)
if (minimize(dist[u + v][v], w + 1))
pq.push(make_pair(w + 1, make_pair(u + v, v)));
if (u - v >= 0)
if (minimize(dist[u - v][v], w + 1))
pq.push(make_pair(w + 1, make_pair(u - v, v)));
if (minimize(dist[u][0], w))
pq.push(make_pair(w, make_pair(u, 0)));
}
}
cout << (dist[b[1]][0] == INF ? -1 : dist[b[1]][0]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
return 0;
}
# | 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... |