Submission #1361333

#TimeUsernameProblemLanguageResultExecution timeMemory
1361333lyra_g13Airplane (NOI23_airplane)C++20
10 / 100
408 ms1114112 KiB
#include <bits/stdc++.h>
using ll = long long;
using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n, m;
  cin >> n >> m;

  vector<ll> a(n);
  for (auto &i : a) {
    cin >> i;
  }
  vector<vector<ll>> adj(n);
  for (int i = 0; i < m; i++) {
    ll u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  for (int i = 0; i < n; i++) {
    adj[i].push_back(i);
  }

  vector<vector<ll>> dp(n, vector<ll>(2001, 1e18));

  dp[0][a[0]] = 0;

  queue<pair<ll, ll>> q;
  q.push({0, 0});

  while (!q.empty()) {
    auto [u, h] = q.front();
    q.pop();

    for (auto v : adj[u]) {
      if (a[v] <= h - 1 and h - 1 >= 0 and dp[v][h - 1] > dp[u][h] + 1) {
        dp[v][h - 1] = dp[u][h] + 1;
        q.push({v, h - 1});
      }
      if (a[v] <= h and dp[v][h] > dp[u][h] + 1) {
        dp[v][h] = dp[u][h] + 1;
        q.push({v, h});
      }
      if (a[v] <= h + 1 and h + 1 <= 2000 and dp[v][h + 1] > dp[u][h] + 1) {
        dp[v][h + 1] = dp[u][h] + 1;
        q.push({v, h + 1});
      }
    }
  }

  cout << dp[n - 1][0] << "\n";
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...