#include <bits/stdc++.h>
#pragma GCC optimise("O3,unroll-loops");
using namespace std;
int n, m, h[200005], dist0[200005], dist1[200005], alt0[200005], alt1[200005];
vector<int> adj[200005];
struct entry {
int d, a, u;
bool operator<(const entry &oth) const {
return d > oth.d;
}
};
void dijkstra(int s, int dist[], int alt[]) {
priority_queue<entry> pq;
pq.push({h[s], h[s], s});
while (pq.size()) {
auto [d, a, u] = pq.top(); pq.pop();
if (dist[u] < d) continue;
dist[u] = d;
alt[u] = a;
for (int v : adj[u]) pq.push({max(d + 1, h[v]), max(alt[u], h[v]), v});
}
}
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
if (n == 1) cout << "0\n", exit(0);
for (int i = 0; i < n; i++) cin >> h[i];
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v; u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dist0, 0x3f, sizeof(dist0));
dijkstra(0, dist0, alt0);
memset(dist1, 0x3f, sizeof(dist1));
dijkstra(n - 1, dist1, alt1);
int ans = 1e9;
for (int i = 0; i < n; i++) {
if (alt0[i] == alt1[i])
ans = min(ans, dist0[i] + dist1[i]);
}
cout << ans << '\n';
}
Compilation message (stderr)
Main.cpp:29:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
29 | main() {
| ^~~~
# | 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... |