# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
212726 | spdskatr | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1097 ms | 70608 KiB |
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 <unordered_map>
#include <unordered_set>
using namespace std;
int N, M, b[30005], p[30005], seen[30005];
unordered_set<int> doges[30005];
unordered_map<int, int> dists;
queue<int> q;
int id(int d, int x) {
return d * 30001 + x;
}
void visit(int pos, int d) {
seen[pos] = 1;
for (int doge : doges[pos]) {
if (dists.find(id(pos, doge)) == dists.end()) {
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]].insert(p[i]);
}
visit(b[0], 0);
while (q.size()) {
int e = q.front(); q.pop();
int loc = e / 30001, power = e % 30001;
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)
# | 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... |