Submission #888465

#TimeUsernameProblemLanguageResultExecution timeMemory
888465adaawfAirplane (NOI23_airplane)C++14
100 / 100
342 ms31280 KiB
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define int long long
int a[200005], f[200005], d[200005];
vector<int> g[200005];
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        f[i] = d[i] = -1;
    }
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    priority_queue<pair<int, int>> q;
    q.push({0, 1});
    f[1] = 0;
    while (!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int x = -p.first, u = p.second;
        for (int w : g[u]) {
            if (f[w] != -1) continue;
            f[w] = max(x + 1, a[w]);
            q.push({-f[w], w});
        }
    }
    q.push({0, n});
    d[n] = 0;
    while (!q.empty()) {
        pair<int, int> p = q.top();
        q.pop();
        int x = -p.first, u = p.second;
        for (int w : g[u]) {
            if (d[w] != -1) continue;
            d[w] = max(x + 1, a[w]);
            q.push({-d[w], w});
        }
    }
    int res = 1e9;
    for (int i = 1; i <= n; i++) {
        res = min(res, max(f[i], d[i]) * 2);
    }
    for (int i = 1; i <= n; i++) {
        for (int w : g[i]) {
            if (f[i] == d[w]) res = min(res, f[i] * 2 + 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...