Submission #520850

# Submission time Handle Problem Language Result Execution time Memory
520850 2022-01-31T07:46:37 Z Alex_tz307 Jakarta Skyscrapers (APIO15_skyscraper) C++17
0 / 100
1 ms 220 KB
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}

void testCase() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  int source = 0, sink = 0;
  for (int i = 0; i < m; ++i) {
    int b, p;
    cin >> b >> p;
    if (0 <= b - p || b + p < n) {
      g[b].emplace_back(p);
    }
    if (i == 0) {
      source = b;
    }
    if (i == 1) {
      sink = b;
    }
  }
  for (int v = 0; v < n; ++v) {
    sort(g[v].begin(), g[v].end());
    g[v].erase(unique(g[v].begin(), g[v].end()), g[v].end());
  }
  queue<pair<int, int>> q;
  vector<map<int, int>> dp(n);
  for (int p : g[source]) {
    q.emplace(source, p);
    dp[source][p] = 0;
  }
  while (!q.empty()) {
    int u, p;
    tie(u, p) = q.front();
    q.pop();
    if (0 <= u - p) {
      if (dp[u - p].find(p) == dp[u - p].end() || dp[u - p][p] > dp[u][p] + 1) {
        dp[u - p][p] = dp[u][p] + 1;
        q.emplace(u - p, p);
      }
    }
    if (u + p < n) {
      if (dp[u + p].find(p) == dp[u + p].end() || dp[u + p][p] > dp[u][p] + 1) {
        dp[u + p][p] = dp[u][p] + 1;
        q.emplace(u + p, p);
      }
    }
    for (int pp : g[u]) {
      if (0 <= u - pp) {
        if (dp[u - pp].find(pp) == dp[u - pp].end() || dp[u - pp][pp] > dp[u][p] + 1) {
          dp[u - pp][pp] = dp[u][p] + 1;
          q.emplace(u - pp, pp);
        }
      }
      if (u + pp < n) {
        if (dp[u + pp].find(pp) == dp[u + pp].end() || dp[u + pp][pp] > dp[u][p] + 1) {
          dp[u + pp][pp] = dp[u][p] + 1;
          q.emplace(u + pp, pp);
        }
      }
    }
  }
  int best = INF;
  for (auto it : dp[sink]) {
    minSelf(best, it.second);
  }
  if (best == INF) {
    best = -1;
  }
  cout << best << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int tests = 1;
  for (int tc = 0; tc < tests; ++tc) {
    testCase();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 220 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -