이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
/* vector<vector<array<int, 2>>> adj(n);
int src, dest;
for(int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
if(i == 0) src = b;
if(i == 1) dest = b;
for(int j = b+p; j < n; j += p)
adj[j-p].push_back({j, 1});
for(int j = b-p; j >= 0; j -= p)
adj[j+p].push_back({j, 1});
}
vector<int> dist(n, INF);
dist[src] = 0;
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
pq.push({0, src});
while(!pq.empty()){
auto [d, node] = pq.top();
pq.pop();
for(auto& [v, nd] : adj[node]){
if(dist[v] <= dist[node] + nd)
continue;
dist[v] = dist[node] + nd;
pq.push({dist[v], v});
}
}
cout << (dist[dest] == INF ? -1 : dist[dest]) << '\n';*/
vector<vector<array<int, 3>>> adj(n);
vector<array<int, 2>> d(n);
int src, dest;
for(int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
d[i] = {b, p};
if(i == 0) src = b;
if(i == 1) dest = b;
for(int j = b+p; j < n; j+=p)
adj[j-p].push_back({j, 1, i});
for(int j = b-p; j >= 0; j-=p)
adj[j+p].push_back({j, 1, i});
}
vector<int> dist(n, INF);
dist[src] = 0;
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> pq;
pq.push({0, src});
while(!pq.empty()){
auto [t, node, edge_source] = pq.top();
pq.pop();
for(auto& [v, nd, ne_s] : adj[node]){
//to access this particular edge, edge_source -> ne_s -> node
//abs(d[ne_s][0] - d[edge_source][0]) / d[edge_source][1] + abs(d[v][0] - d[ne_s][0]) / d[ne_s][1]
int inc = (abs(d[ne_s][0] - d[edge_source][0]))/d[edge_source][1] + abs(d[v][0] - d[ne_s][0])/d[ne_s][1];
if(dist[v] <= inc)
continue;
dist[v] = inc;
pq.push({inc, v, ne_s});
}
}
cout << (dist[dest] == INF ? -1 : dist[dest]) << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'int32_t main()':
skyscraper.cpp:79:20: warning: 'dest' may be used uninitialized in this function [-Wmaybe-uninitialized]
79 | cout << (dist[dest] == INF ? -1 : dist[dest]) << '\n';
| ^
skyscraper.cpp:61:10: warning: 'src' may be used uninitialized in this function [-Wmaybe-uninitialized]
61 | dist[src] = 0;
| ^
# | 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... |