Submission #1182786

#TimeUsernameProblemLanguageResultExecution timeMemory
1182786Ronin13Airplane (NOI23_airplane)C++20
100 / 100
328 ms24884 KiB
#include <bits/stdc++.h>
#define ll long long 
#define ull unsigned ll
#define f first
#define s second 
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;

const int nmax = 500001;

vector <pll> Dijkstra(int s, int n, vector<vector<int>>&g, vector<ll>&a) {
    vector <pll> d(n + 1, make_pair(1e18, 1e18));
    priority_queue<pll,vector<pll>, greater<pll>>pq;
    vector <bool> used(n + 1, false);
    d[s] = {0, 0};
    pq.push({0, s});
    while(!pq.empty()) {
        int v = pq.top().s;
         pq.pop();
         if(used[v]) continue;
         used[v] = true;
         for(auto to : g[v]) {
            ll nl = max(d[v].f + 1, a[to]);
            ll nh = max(d[v].s, a[to]);
            if(d[to] > make_pair(nl, nh)) {
                d[to] = {nl, nh};
                pq.push({d[to].f, to});
            }
         }
    }
    return d;
}

int main() {
    int n; cin >> n;
    int m; cin >>m;
    vector <ll>  a(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    
    vector <vector<int>>g(n + 1);
    for(int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    vector<pll>d1 = Dijkstra(1, n, g, a);
    vector <pll> d2 = Dijkstra(n, n, g, a);
    ll ans = 1e18;
    for(int i = 1; i <= n; i++) {
        ll x = d1[i].f + d2[i].f;
        if(d1[i].s == a[i] && d2[i].s == a[i])
        ans = min(ans, x);
    }
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...