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;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> G(N);
vector<int> B(M), P(M);
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
for (int j = B[i] % P[i]; j < N; j += P[i]) {
G[B[i]].push_back({j, abs(B[i] - j) / P[i]});
}
}
min_heap<pair<int, int>> Q;
vector<int> dist(N, INT_MAX);
dist[B[0]] = 0;
Q.push({0, B[0]});
while (!Q.empty()) {
auto [d, u] = Q.top();
Q.pop();
if (d > dist[u]) {
continue;
}
for (auto [v, w] : G[u]) {
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
Q.push({dist[v], v});
}
}
}
cout << (dist[B[1]] == INT_MAX ? -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... |