This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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");
}
Compilation message (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... |