# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
63823 | kingpig9 | Jakarta Skyscrapers (APIO15_skyscraper) | C++11 | 171 ms | 16616 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 long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 30010;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
int N, M;
int S, T;
vector<int> jump[MAXN];
int dist[MAXN];
bool vis[MAXN];
int main() {
scanf("%d %d", &N, &M);
for (int i = 0; i < M; i++) {
int x, p;
scanf("%d %d", &x, &p);
jump[x].push_back(p);
if (i == 0) {
S = x;
} else if (i == 1) {
T = x;
}
}
for (int i = 0; i < N; i++) {
sort(all(jump[i]));
jump[i].resize(unique(all(jump[i])) - jump[i].begin());
}
for (int i = 0; i < N; i++) {
dist[i] = INT_MAX / 2;
}
dist[S] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({0, S});
while (!pq.empty()) {
int w = pq.top().fi;
int u = pq.top().se;
pq.pop();
if (u == T) {
printf("%d\n", w);
return 0;
}
if (vis[u]) {
continue;
}
vis[u] = true;
for (int p : jump[u]) {
for (int i = 1; u + i * p < N; i++) {
int v = u + i * p;
int nw = w + i;
if (dist[v] > nw) {
dist[v] = nw;
pq.push({nw, v});
}
if (binary_search(all(jump[v]), p)) {
break;
}
}
for (int i = 1; u - i * p >= 0; i++) {
int v = u - i * p;
int nw = w + i;
if (dist[v] > nw) {
dist[v] = nw;
pq.push({nw, v});
}
if (binary_search(all(jump[v]), p)) {
break;
}
}
}
}
puts("-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... |