This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
template <class A, class B>
bool minl(A &a, B &b){
return a > b ? (a = b), true : false;
}
int n, a[N];
vector <int> adj[N];
void dijkstra(int src, int d[]){
fill(d + 1, d + n + 1, mod);
using T = pair <int, int>;
priority_queue <T, vector <T>, greater <T>> qu;
d[src] = 0; qu.emplace(0, src);
while (qu.size()){
auto [du, u] = qu.top(); qu.pop();
if (d[u] != du) continue;
for (int v : adj[u]){
if (minl(d[v], max(d[u] + 1, a[v]))){
qu.emplace(d[v], v);
}
}
}
}
int m, up[N], down[N];
void solve(){
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;
adj[u].push_back(v);
adj[v].push_back(u);
}
dijkstra(1, up);
dijkstra(n, down);
int res = mod;
for (int i = 1; i <= n; ++ i) {
res = min(res, up[i] + down[i]);
for (int j : adj[i]) res = min(res, max(up[i], down[j]) * 2 + 1);
}
cout << res;
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen("pqh.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("pqh.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |