# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
41506 | fest | Jakarta Skyscrapers (APIO15_skyscraper) | C++14 | 30 ms | 30188 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.
// fest
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define y1 dasdasfasfas
#define x1 wqdadfasfasfas
#define All(c) c.begin(), c.end()
#define SZ(A) (int((A).size()))
#define umap unordered_map
#define FILENAME ""
#define __ fflush(stdout)
typedef long long ll;
typedef long double ld;
using namespace std;
void FREOPEN() {
#ifdef COMP
freopen(".in", "r", stdin);
freopen("2.out", "w", stdout);
#else
freopen(FILENAME".in", "r", stdin);
freopen(FILENAME".out", "w", stdout);
#endif
}
inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; }
const int N = 3001, inf = 1e9 * 2, MOD = (int)1e9 + 7;
char CH[N];
const ll INF = 1e18;
const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
bool was[N][N];
int dp[N][N];
vector<int> have[N];
int main() {
int n, m;
scanf("%d %d", &n, &m);
int start = 0, finish = 0;
for (int i = 0; i < m; i++) {
int b, p;
scanf("%d %d", &b, &p);
have[b].pb(p);
if (i == 0) start = b;
if (i == 1) finish = b;
}
for (int i = 0; i < n; i++) for (int j = 0; j < N; j++) dp[i][j] = inf;
vector<pair<int, int> > vers;
for (auto i : have[start]) dp[start][i] = 0, was[start][i] = 1, vers.pb({start, i});
int l = 0;
while (l < SZ(vers)) {
int v = vers[l].F;
int p = vers[l].S;
l++;
if (v + p < n) {
if (!was[v + p][p]) {
was[v + p][p] = 1;
dp[v + p][p] = dp[v][p] + 1;
vers.pb({v + p, p});
if (!have[v + p].empty()) {
if (!was[v + p][have[v + p][0]]) {
for (auto i : have[v + p]) {
was[v + p][i] = 1;
dp[v + p][i] = dp[v][p] + 1;
vers.pb({v + p, i});
}
}
}
}
}
if (v - p >= 0) {
if (!was[v - p][p]) {
was[v - p][p] = 1;
dp[v - p][p] = dp[v][p] + 1;
vers.pb({v - p, p});
if (!have[v - p].empty()) {
if (!was[v - p][have[v - p][0]]) {
for (auto i : have[v - p]) {
was[v - p][i] = 1;
dp[v - p][i] = dp[v][p] + 1;
vers.pb({v - p, i});
}
}
}
}
}
}
int out = inf;
for (int i = 0; i < N; i++) out = min(out, dp[finish][i]);
if (out == inf) out = -1;
cout << out;
return 0;
}
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... |