이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
using namespace std;
int main () {
int n, m;
cin >> n >> m;
int rt = sqrt(n)+1;
vector< pair<int, int> > adj[n*rt];
int start, end;
for (int i=0; i<n; i++) {
for (int j=1; j<rt; j++) {
adj[i*rt+j].push_back({i*rt, 0});
if (i-j >= 0) adj[i*rt+j].push_back({(i-j)*rt+j, 1});
if (i+j < n) adj[i*rt+j].push_back({(i+j)*rt+j, 1});
}
}
int bi, pi;
for (int i=0; i<m; i++) {
cin >> bi >> pi;
if (i == 0) start = bi;
if (i == 1) end = bi;
if (pi < rt) {
adj[bi*rt].push_back({bi*rt+pi, 0});
} else {
for (int j=1; j<n; j++) {
if (bi-j*pi < 0 && bi+j*pi >= n) break;
if (bi-j*pi >= 0) adj[bi*rt].push_back({(bi-j*pi)*rt, j});
if (bi+j*pi < n) adj[bi*rt].push_back({(bi+j*pi)*rt, j});
}
}
}
bool vis[n*rt];
long long dist[n*rt];
for (int i=0; i<n*rt; i++) {
vis[i] = false;
dist[i] = 1e16;
}
set< pair<long long, int> > ord;
dist[start*rt] = 0;
ord.insert({0, start*rt});
while (ord.size() > 0) {
pair<long long, int> cur = *ord.begin();
ord.erase(ord.begin());
vis[cur.second] = true;
for (auto i: adj[cur.second]) {
if (cur.first+i.second < dist[i.first]) {
ord.erase({dist[i.first], i.first});
dist[i.first] = cur.first+i.second;
ord.insert({dist[i.first], i.first});
}
}
}
if (dist[end*rt] == 1e16) {
cout << -1 << endl;
} else {
cout << dist[end*rt] << endl;
}
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:35:7: warning: variable 'vis' set but not used [-Wunused-but-set-variable]
bool vis[n*rt];
^~~
skyscraper.cpp:42:12: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
dist[start*rt] = 0;
~~~~~^~~
skyscraper.cpp:56:14: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (dist[end*rt] == 1e16) {
~~~^~~
# | 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... |