이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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 &step : a[u])
d[u][step] = 0;
d[u][0] = 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;
a[b].push_back(p);
if (i == 1)
init = b;
else if (i == 2)
goal = b;
}
cout << find(init);
}
return 0;
}
컴파일 시 표준 에러 (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... |