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>
#define ll long long
using namespace std;
const int N = 3e4 + 10;
vector<vector<int>> g(N);
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m; cin >> n >> m;
int f, s;
for(short i = 0; i < m; i++) {
int x, y; cin >> x >> y;
if(i == 0) f = x;
if(i == 1) s = x;
if(y == 0) continue;
g[x].push_back(y);
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> c;
vector<int> d(n, 2e9);
d[f] = 0;
c.push({0, f});
while(!c.empty()) {
int u = c.top().second, w = c.top().first;
c.pop();
if(d[u] != w) continue;
if(u == s) {
cout << d[s];
return 0;
}
for(int x : g[u]) {
for(int j = u % x; j < n; j += x) {
if(d[j] > d[u] + abs(j - u) / x) {
d[j] = d[u] + abs(j - u) / x;
c.push({d[j], j});
}
}
}
}
cout << -1;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:20:5: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
20 | d[f] = 0;
| ^
skyscraper.cpp:26:3: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(u == s) {
| ^~
# | 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... |