#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, INT_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] == INT_MAX ? -1 : dis[tx]);
}
Compilation message
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] == INT_MAX ? -1 : dis[tx]);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
0 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |