#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
#define ull unsigned long long
#define llll __int128
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define coutpair(p) cout << p.first << " " << p.second << "|"
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll n, m;
ll a[200005];
vector<pll> adj[200005];
vector<ll> sdist, edist;
bool svis[200005], evis[200005];
vector<ll> dijkstra(int st){
priority_queue<pll, vector<pll>, greater<pll>> pq;
vector<ll> dist(200005); bool vis[200005];
for (int i=0; i<=n; i++){
dist[i] = LLONG_MAX;
vis[i] = false;
}
dist[st] = 0; vis[st] = false;
pq.push({0, st});
while (!pq.empty()){
ll weight = pq.top().first, cur = pq.top().second;
pq.pop();
if (dist[cur] < weight) continue;
vis[cur] = true;
for (auto [w, node]: adj[cur]){
ll nd = max(w, dist[cur]+1);
if (dist[node] > nd){
dist[node] = nd;
pq.push({dist[node], node});
}
}
}
return dist;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=1; i<=n; i++) cin >> a[i];
for (int i=0; i<m; i++){
ll u, v; cin >> u >> v;
adj[u].push_back({a[v], v});
adj[v].push_back({a[u], u});
}
sdist = dijkstra(1);
edist = dijkstra(n);
ll ans = LLONG_MAX;
for (int i=1; i<=n; i++){
//cout << i << " " << sdist[i] << " " << edist[i] << "\n";
ans = min(ans, max(sdist[i], edist[i])*2 - (ll)(abs(sdist[i]-edist[i]) == 1));
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |