# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
249226 | rdd6584 | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 1092 ms | 72952 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 <bits/stdc++.h>
using namespace std;
typedef pair<int ,int> pii;
char visit[30000];
map<int,int> dist[30000];
vector<int> v[30000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
int st, ed;
for (int i = 0; i < m; i++) {
int a,b;
scanf("%d %d" ,&a, &b);
v[a].push_back(b);
if (i==0) st = a;
else if (i==1) ed = a;
}
queue<pii> q;
for (int i : v[st]) dist[st][i] = 0, q.push({st, i});
while (!q.empty()) {
pii tv = q.front();
q.pop();
if (tv.first == ed) return !printf("%d", dist[tv.first][tv.second]);
int ne = tv.first-tv.second;
int me = dist[tv.first][tv.second];
if (ne >= 0) {
if (dist[ne].find(tv.second) == dist[ne].end()) {
int flag = 0;
if (!visit[ne]) {
visit[ne] = 1;
for (int i : v[ne]) {
dist[ne][i] = me+1;
q.push({ne, i});
flag |= i==tv.second;
}
}
if (!flag) {
q.push({ne, tv.second});
dist[ne][tv.second] = me + 1;
}
}
}
ne = tv.first+tv.second;
if (ne < n) {
if (dist[ne].find(tv.second) == dist[ne].end()) {
int flag = 0;
if (!visit[ne]) {
visit[ne] = 1;
for (int i : v[ne]) {
dist[ne][i] = me+1;
q.push({ne, i});
flag |= i==tv.second;
}
}
if (!flag) {
q.push({ne, tv.second});
dist[ne][tv.second] = me + 1;
}
}
}
}
printf("-1");
}
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... |