#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> X(m), P(m);
for (int i = 0; i < m; i++)
cin >> X[i] >> P[i];
int mn = INT_MAX;
vector<int> dis(n, INT_MAX); dis[X[0]] = 0;
priority_queue<pair<int, pair<int, int>>> q; q.push({0, {X[0], P[0]}});
while (!q.empty()) {
int d = q.top().first;
auto [x, p] = q.top().second; q.pop();
for (int i = x; i >= 0; i -= p) {
for (int j = 0; j < m; j++) {
if (!(abs(X[j] - i) % P[j]) && dis[x] + (x - i) / p + abs(X[j] - i) / P[j] < dis[i]) {
dis[i] = dis[x] + (x - i) / p + abs(X[j] - i) / P[j];
q.push({-dis[i], {i, P[j]}});
if (j == 1) mn = min(mn, dis[i]);
}
}
}
for (int i = x; i < n; i += p) {
for (int j = 0; j < m; j++) {
if (!(abs(X[j] - i) % P[j]) && dis[x] + (i - x) / p + abs(X[j] - i) / P[j] < dis[i]) {
dis[i] = dis[x] + (i - x) / p + abs(X[j] - i) / P[j];
q.push({-dis[i], {i, P[j]}});
if (j == 1) mn = min(mn, dis[i]);
}
}
}
}
cout << (mn == INT_MAX ? -1 : mn);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:20:7: warning: unused variable 'd' [-Wunused-variable]
20 | int d = q.top().first;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |