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 dbg(x) cerr << #x << ": " << x << endl;
const int MAX_N = 30000 + 5;
const ll INF = 1e16;
vector<pair<int, int>> g[MAX_N];
ll dist[MAX_N];
bool used[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int s, t;
for (int i = 0; i < m; ++i) {
int v, k;
cin >> v >> k;
if (i == 0) {
s = v;
}
if (i == 1) {
t = v;
}
for (int u = v + k; u < n; u += k) {
g[v].emplace_back(u, (u - v) / k);
}
for (int u = v - k; u >= 0; u -= k) {
g[v].emplace_back(u, (v - u) / k);
}
}
fill_n(dist, n, INF);
priority_queue<pair<ll, int>> pq;
pq.emplace(0, s);
while (!pq.empty()) {
auto [curr_dist, v] = pq.top();
pq.pop();
if (used[v]) continue;
used[v] = true;
dist[v] = curr_dist;
if (dist[v] == INF) break;
if (v == t) break;
for (const auto& [u, w] : g[v]) {
if (used[u]) continue;
if (dist[u] <= curr_dist + w) continue;
dist[u] = curr_dist + w;
pq.emplace(dist[u], u);
}
}
if (!used[t]) {
cout << "-1\n";
} else {
cout << dist[t] << '\n';
}
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:51:9: warning: 't' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | if (v == t) break;
| ^~
# | 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... |