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;
using ll = long long;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m;
cin >> n >> m;
int tx;
vector<int> p(n), dis(n, LLONG_MAX);
priority_queue<pii> q;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
p[x] = y;
if (!i) {
dis[x] = 0;
q.push({0, x});
}
else if (i == 1) tx = x;
}
while (!q.empty()) {
auto [d, x] = q.top(); d = -d; q.pop();
if (d != dis[x]) continue;
for (int i = x; i < n; i += p[x]) {
if (p[i] && d + abs(i - x) / p[x] < dis[i]) {
dis[i] = d + abs(i - x) / p[x];
q.push({-dis[i], i});
}
}
for (int i = x; i >= 0; i -= p[x]) {
if (p[i] && d + abs(i - x) / p[x] < dis[i]) {
dis[i] = d + abs(i - x) / p[x];
q.push({-dis[i], i});
}
}
}
cout << (dis[tx] == LLONG_MAX ? -1 : dis[tx]);
}
Compilation message (stderr)
sculpture.cpp: In function 'int32_t main()':
sculpture.cpp:53:20: warning: 'tx' may be used uninitialized in this function [-Wmaybe-uninitialized]
53 | cout << (dis[tx] == LLONG_MAX ? -1 : dis[tx]);
| ^
# | 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... |