#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 3e4+4;
int n, m;
vi doges[N];
void sol() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
int x, p; cin >> x >> p; x++;
doges[x].pb(p);
}
priority_queue<pii, vector<pii>, greater<>> q;
q.push({0, 1});
vi dis(n+1, inf); dis[1] = 0;
while (!q.empty()) {
pii f = q.top(); q.pop();
int src = f.s, dist = f.f;
if (dist != dis[src]) continue;
if (src == 2) {
cout << dist << endl;
return;
}
for (auto x : doges[src]) {
int ct = 1;
for (int ch = src+x; ch <= n; ch += x) {
if (dis[ch] > dis[src] + ct) {
dis[ch] = dis[src] + ct;
q.push({dis[ch], ch});
}
ct++;
}
ct = 1;
for (int ch = src-x; ch >= 1; ch -= x) {
if (dis[ch] > dis[src] + ct) {
dis[ch] = dis[src] + ct;
q.push({dis[ch], ch});
}
ct++;
}
}
}
cout << -1 << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1268 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1168 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |