이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <functional>
#include <utility>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
int N, M, b[30005], p[30005], seen[30005];
vector<int> doges[30005];
map<int, int> dists;
queue<int> q;
int id(int d, int x) {
return d * 30001 + x;
}
void visit(int pos, int d) {
//printf("visit(%d, %d)\n", pos, d);
seen[pos] = 1;
for (int doge : doges[pos]) {
dists[id(pos, doge)] = d;
q.push(id(pos, doge));
}
}
int main() {
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
scanf("%d %d", b+i, p+i);
doges[b[i]].push_back(p[i]);
}
visit(b[0], 0);
while (q.size()) {
int e = q.front(); q.pop();
int loc = e / 30001, power = e % 30001;
//printf("At (%d, %d) (dist %d)\n", loc, power, dists[e]);
if (loc == b[1]) {
printf("%d\n", dists[e]);
return 0;
}
if (loc - power >= 0) {
int pos = loc - power;
int d = power;
if (!seen[pos]) {
visit(pos, dists[e] + 1);
}
if (dists.find(id(pos, d)) == dists.end()) {
dists[id(pos, d)] = dists[e] + 1;
q.push(id(pos, d));
}
}
if (loc + power < N) {
int pos = loc + power;
int d = power;
if (!seen[pos]) {
visit(pos, dists[e] + 1);
}
if (dists.find(id(pos, d)) == dists.end()) {
dists[id(pos, d)] = dists[e] + 1;
q.push(id(pos, d));
}
}
}
printf("-1\n");
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:36:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", b+i, p+i);
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |