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<pair<int, int>>> g(N);
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m; cin >> n >> m;
int f, s;
for(int i = 0; i < m; i++) {
int x, y; cin >> x >> y;
if(i == 0) f = x;
if(i == 1) s = x;
for(int j = x + y; j < n; j += y) {
g[x].push_back({j, (j - x) / y});
}
for(int j = x - y; j >= 0; j -= y) {
g[x].push_back({j, (x - j) / y});
}
}
set<pair<ll, int>> c;
vector<ll> d(n, 1e15);
d[f] = 0;
c.insert({0, f});
while(!c.empty()) {
ll w = c.begin() -> first;
int v = c.begin() -> second;
c.erase(c.begin());
if(d[v] != w) continue;
for(pair<int, int> to : g[v]) {
if(d[to.first] > d[v] + to.second) {
d[to.first] = d[v] + to.second;
c.insert({d[to.first], to.first});
}
}
}
cout << (d[s] == 1e15 ? -1 : d[s]);
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:24:5: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
24 | d[f] = 0;
| ^
skyscraper.cpp:38:14: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | cout << (d[s] == 1e15 ? -1 : d[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... |