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;
const int MAX_N = 30000;
const int C = 300;
vector<pair<int, int>> G[MAX_N * C];
int B[MAX_N], P[MAX_N];
int D[MAX_N * C];
void connect(int u, int v, int w, bool directed = false) {
G[u].push_back({v, w});
if (!directed) {
G[v].push_back({u, w});
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
if (P[i] >= C) {
for (int j = B[i] % P[i]; j < N; j += P[i]) {
connect(B[i], j, abs(j - B[i]) / P[i], true);
}
} else {
connect(B[i], B[i] + P[i] * N, 0, true);
}
}
for (int i = 0; i < N; i++) {
for (int power = 1; power < C; power++) {
if (i - power >= 0) {
connect(i + power * N, i + power * (N - 1), 1, true);
connect(i + power * N, i - power, 1, true);
}
if (i + power < N) {
connect(i + power * N, i + power * (N + 1), 1, true);
connect(i + power * N, i + power, 1, true);
}
}
}
fill(D, D + MAX_N, INT_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
Q.push({D[B[0]] = 0, B[0]});
while (!Q.empty()) {
auto [d, u] = Q.top();
Q.pop();
if (d > D[u]) {
continue;
}
for (auto [v, w] : G[u]) {
if (D[u] + w < D[v]) {
Q.push({D[v] = D[u] + w, v});
}
}
}
cout << (D[B[1]] == INT_MAX ? -1 : D[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... |