이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <queue>
#include <cstdio>
#include <tuple>
#include <cstring>
#define pii pair<int, int>
#define piii tuple<int, int, int>
#define mt make_tuple
#define push_back emplace_back
using namespace std;
int N, M;
pii A[30005];
int dist[30005][180];
vector<pii> adj[30005];
vector<int> dog[30005];
void BFS() {
priority_queue<piii> pq;
memset(dist, 0x3f3f3f3f, sizeof(dist));
pq.push(make_tuple(0, A[0].first, 0));
dist[A[0].first][0] = 0;
int u, mov; int trav;
while(!pq.empty()) {
tie(trav, u, mov) = pq.top();
pq.pop();
if(dist[u][mov] < trav) continue;
if(mov != 0) {
if(u - mov >= 0 && trav + 1 < dist[u - mov][mov]) {
dist[u - mov][mov] = trav + 1;
pq.push(make_tuple(trav + 1, u - mov, mov));
}
if(u + mov <= N && trav + 1 < dist[u + mov][mov]) {
dist[u + mov][mov] = trav + 1;
pq.push(make_tuple(trav + 1, u + mov, mov));
}
if(dist[u][0] > trav) {
dist[u][0] = trav;
pq.push(make_tuple(trav, u, 0));
}
}else {
int v, w;
for(pii now : adj[u]) {
v = now.first, w = now.second + trav;
if(w < dist[v][0]) {
dist[v][0] = w;
pq.push(make_tuple(w, v, 0));
}
}
for(int v : dog[u]) {
if(trav < dist[u][v]) {
dist[u][v] = trav;
pq.push(make_tuple(trav, u, v));
}
}
adj[u].clear();
dog[u].clear();
}
}
if(dist[A[1].first][0] == 0x3f3f3f3f) puts("-1");
else printf("%d\n", dist[A[1].first][0]);
}
int main() {
// freopen("out.txt", "w", stdout);
scanf("%d %d", &N, &M);
int pos, mov;
for(int i = 0; i < M; i++) {
scanf("%d %d", &pos, &mov);
A[i] = make_pair(pos, mov);
if(mov * mov > N) {
for(int i = pos - mov, j = 1; i >= 0; i -= mov, j++) {
adj[pos].push_back(i, j);
}
for(int i = pos + mov, j = 1; i <= N; i += mov, j++) {
adj[pos].push_back(i, j);
}
}else {
dog[pos].push_back(mov);
}
}
BFS();
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:65:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &pos, &mov);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |