# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
786108 | devariaota | Jakarta Skyscrapers (APIO15_skyscraper) | C++17 | 26 ms | 34636 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;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
queue<pair<int, int>> q;
vector<int> adj[30005];
int dist[2005][2005];
bool vis[2005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
int s, f;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
if (i == 0) s = u;
if (i == 1) f = u;
}
for (int i = 0; i <= 2000; i++) {
for (int j = 0; j <= 2000; j++) dist[i][j] = -1;
}
for (auto x : adj[s]) {
q.push({s, x});
dist[s][x] = 0;
}
vis[s] = 1;
while (!q.empty()) {
int x = q.front().fi, d = q.front().se;
q.pop();
if (x == f) {
cout << dist[x][d] << "\n";
return 0;
}
if (x - d >= 0 and !vis[x - d]) {
vis[x - d] = 1;
for (auto u : adj[x - d]) {
q.push({x - d, u});
dist[x - d][u] = dist[x][d] + 1;
}
}
if (x - d >= 0 and dist[x - d][d] == -1) {
q.push({x - d, d});
dist[x - d][d] = dist[x][d] + 1;
}
if (x + d < n and !vis[x + d]) {
vis[x + d] = 1;
for (auto u : adj[x + d]) {
q.push({x + d, u});
dist[x + d][u] = dist[x][d] + 1;
}
}
if (x + d < n and dist[x + d][d] == -1) {
q.push({x + d, d});
dist[x + d][d] = dist[x][d] + 1;
}
}
cout << -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... |