#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> dis(n, LLONG_MAX);
vector<set<int>> p(n);
priority_queue<pii> q;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
p[x].insert(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 (auto j : p[x]) {
for (int i = x + j; i < n && p[i].find(j) == p[i].end(); i += j) {
if (!p[i].empty() && d + abs(i - x) / j < dis[i]) {
dis[i] = d + abs(i - x) / j;
q.push({-dis[i], i});
}
}
for (int i = x - j; i >= 0 && p[i].find(j) == p[i].end(); i -= j) {
if (!p[i].empty() && d + abs(i - x) / j < dis[i]) {
dis[i] = d + abs(i - x) / j;
q.push({-dis[i], i});
}
}
}
}
cout << (dis[tx] == LLONG_MAX ? -1 : dis[tx]);
}
Compilation message
bridge.cpp: In function 'int32_t main()':
bridge.cpp:56:20: warning: 'tx' may be used uninitialized in this function [-Wmaybe-uninitialized]
56 | cout << (dis[tx] == LLONG_MAX ? -1 : dis[tx]);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |