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 N = 2003;
const int M = 30004;
int n, m;
int ans;
int b[M];
int p[M];
int cost[M];
vector<pair<int, int>> g[M];
inline void dij() {
memset(cost, -1, sizeof cost);
cost[b[0]] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int >>> pq;
pq.emplace(0, b[0]);
while (!pq.empty()) {
auto src = pq.top();
pq.pop();
if (cost[src.second] != src.first) {
continue;
}
// cout << src.first << ' ' << src.second << '\n';
if (src.second == b[1]) {
ans = min(ans, src.first);
}
for (auto [u, w] : g[src.second]) {
if (cost[u] == -1 || cost[u] > src.first + w) {
cost[u] = src.first + w;
pq.emplace(cost[u], u);
}
}
}
}
signed main() {
ans = INT_MAX;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> b[i] >> p[i];
for (int j = 1; j < N; ++j) {
int r = b[i] + j * p[i];
int l = b[i] - j * p[i];
if (l >= 0){
g[b[i]].emplace_back(l, j);
}
if (r < n) {
g[b[i]].emplace_back(r, j);
}
}
}
dij();
cout << (ans == INT_MAX ? -1 : ans) << '\n';
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... |