This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/APIO15_skyscraper
#include <bits/stdc++.h>
using namespace std;
#define Fname ((string) "io")
#define int long long
#define ii pair <int, int>
#define iii pair <int, ii>
#define fi first
#define se second
#define endl '\n'
const int INF = 1e16;
const int N = 3e3 + 5;
int n, m, d[N][N], goal;
vector <int> a[N];
priority_queue <iii, vector <iii>, greater <iii>> pq;
int find(int u) {
for (int i = 1; i <= n; i++)
for (int j = 0; j <= n; j++)
d[i][j] = INF;
pq.push({0, {u, 0}});
for (int j = 0; j <= n; j++)
d[u][j] = 0;
while (pq.size()) {
u = pq.top().se.fi;
int step = pq.top().se.se, total = pq.top().fi;
pq.pop();
if (u == goal)
return total;
if (total != d[u][step])
continue;
a[u].push_back(step);
for (int &step : a[u]) {
if (u + step <= n && d[u + step][step] > total + 1) {
d[u + step][step] = total + 1;
pq.push({total + 1, {u + step, step}});
}
if (u - step > 0 && d[u - step][step] > total + 1) {
d[u - step][step] = total + 1;
pq.push({total + 1, {u - step, step}});
}
}
a[u].pop_back();
}
return -1;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef lan_ngu
freopen((Fname + ".inp").c_str(), "r", stdin);
freopen((Fname + ".out").c_str(), "w", stdout);
#endif
// int _nt; cin >> _nt;
int _nt = 1;
while (_nt--) {
cin >> n >> m;
int init;
for (int i = 1; i <= m; i++) {
int b, p; cin >> b >> p;
b++;
a[b].push_back(p);
if (i == 1)
init = b;
else if (i == 2)
goal = b;
}
cout << find(init);
}
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:72:20: warning: 'init' may be used uninitialized in this function [-Wmaybe-uninitialized]
72 | cout << find(init);
| ^
# | 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... |