Submission #891925

#TimeUsernameProblemLanguageResultExecution timeMemory
891925peterandvoiAirplane (NOI23_airplane)C++17
100 / 100
252 ms24132 KiB
#include <bits/stdc++.h>

using namespace std;

#define db(...) " [" << #__VA_ARGS__ << " : " << (__VA_ARGS__) << "] "

#define N   200005

int n, m;
int a[N];
vector<int> g[N];
// nhan xet: bay len den 1 diem mid sau do di xuong
int up[N]; // bay len
int down[N]; // bay xuong

template<class A, class B>
bool mini(A &a, const B &b) {
  return a > b ? (a = b), true : false;
}

void dijkstra(int src, int d[]) {
  fill(d + 1, d + n + 1, 2e8);
  d[src] = 0;
  using T = pair<int, int>;
  priority_queue<T, vector<T>, greater<T>> pq;
  pq.emplace(0, src);
  while (pq.size()) {
    auto [c, u] = pq.top();
    pq.pop();
    if (c != d[u]) {
      continue;
    }
    for (int v : g[u]) {
      if (mini(d[v], max(d[u] + 1, a[v]))) {
        pq.emplace(d[v], v);
      }
    }
  }
}
  
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1, u, v; i <= m; ++i) {
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  dijkstra(1, up);
  dijkstra(n, down);
  int res = 1e9;
  for (int i = 1; i <= n; ++i) {
    res = min(res, 2 * max(up[i], down[i]));
  }
  // giao nhau tai (u, v)
  for (int u = 1; u <= n; ++u) {
    for (int v : g[u]) {
      res = min(res, 2 * max(up[u], down[v]) + 1);
    }
  }
  cout << res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...