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;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 30000 + 10;
const ll SQ = 170;
const ll INF = 1e18;
vector<int> vec[MAXN];
ll dist[MAXN][SQ], B[MAXN], P[MAXN], n, m;
bool F[MAXN][SQ];
priority_queue<plll, vector<plll>, greater<plll>> pq;
inline void update(int v, int p, int d) {
if (d < dist[v][p]) {
dist[v][p] = d;
pq.push({d, {v, p}});
}
}
inline void dijkstra() {
while (!pq.empty()) {
int v = pq.top().Y.X, p = pq.top().Y.Y, d = pq.top().X;
pq.pop();
if (d != dist[v][p]) continue;
if (p) {
update(v, 0, d);
if (v + p < n) update(v + p, p, d + 1);
if (v - p >= 0) update(v - p, p, d + 1);
} else {
for (int i = 1; i < SQ; i++)
if (F[v][i])
update(v, i, d);
for (int e : vec[v]) {
int ind = 0;
for (int u = v + e; u < n; u += e)
ind++, update(u, 0, d + ind); //
ind = 0;
for (int u = v - e; u >= 0; u -= e)
ind++, update(u, 0, d + ind); //
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> B[i] >> P[i];
if (P[i] >= SQ) vec[B[i]].push_back(P[i]);
else F[B[i]][P[i]] = true;
}
for (int i = 0; i < n; i++)
memset(dist[i], 63, sizeof dist[i]);
pq.push({0, {B[0], 0}});
dist[B[0]][0] = 0;
dijkstra();
cout << (dist[B[1]][0] > INF ? -1 : dist[B[1]][0]) << endl;
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... |