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;
const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int) (x).size())
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = 30303;
const int K = 176;
const int V = N * K;
int n, m;
int b[N], p[N];
int dist[V];
bool used[V];
vector<int> which[N];
priority_queue<pair<int, int>> q;
void go(int v, int u, int len) {
if (mini(dist[u], dist[v] + len)) {
q.push({-dist[u], u});
}
}
void run() {
cin >> n >> m;
rep(i, 0, m) {
cin >> b[i] >> p[i];
which[b[i]].pb(p[i]);
}
fill(dist, dist + V, INF);
dist[b[0]] = 0;
q.push({0, b[0]});
while (sz(q)) {
int v = q.top().second;
q.pop();
if (used[v]) continue;
used[v] = 1;
if (v < n) {
for (int step : which[v]) {
if (step < K) {
go(v, v + step * n, 0);
} else {
rep(j, 1, N) {
int u = v + step * j;
if (u >= n) {
break;
}
go(v, u, j);
}
rep(j, 1, N) {
int u = v - step * j;
if (u <= 0) {
break;
}
go(v, u, j);
}
}
}
} else {
int step = v / n;
int pos = v % n;
if (pos + step < n) {
go(v, step * n + pos + step, 1);
}
if (pos - step >= 0) {
go(v, step * n + pos - step, 1);
}
go(v, pos, 0);
}
}
cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << "\n";
}
# | 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... |