이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n, m; cin >> n >> m;
vector <pair<int, int>> a(m);
set <int> st[n];
int target;
vector <int> d(n, INF);
priority_queue <pair<int, int>> pq;
for (int i = 0; i < m; i++){
cin >> a[i].f >> a[i].s;
if (i == 0) {
d[a[i].f] = 0;
pq.push({0, a[i].f});
}
if (i != 1) st[a[i].f].insert(a[i].s);
else target = a[i].f;
}
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < n; i++){
for (auto x : st[i]){
for (int j = 1; ; j++){
int go = x * j + i;
if (go >= n || go < 0) break;
adj[i].push_back({go, j});
}
for (int j = 1; ; j++){
int go = i - x * j;
if (go < 0 || go >= n) break;
adj[i].push_back({go, j});
}
}
}
while (!pq.empty()){
auto pi = pq.top();
pq.pop();
int u = pi.second;
int ds = -pi.first;
if (d[u] != ds) continue;
for (auto [v, w] : adj[u]){
if (d[v] > d[u] + w){
d[v] = d[u] + w;
pq.push({-d[v], v});
}
}
}
// cout << target << "\n";
if (d[target] == INF) cout << -1 << "\n";
else cout << d[target] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'void Solve()':
skyscraper.cpp:65:17: warning: 'target' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | if (d[target] == INF) cout << -1 << "\n";
| ^
# | 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... |