This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e18, mod = 1e9 + 7;
int n, m, k, h[N];
string s;
vector<int> adj[N];
int dist[N][2];
void dijkstra(int s, int source) {
foru(i, 0, n + 1) dist[i][source] = oo;
priority_queue <pair <long long, int>> pq;
pq.push(make_pair(dist[s][source] = 0, s));
while (!pq.empty()) {
pair<long long, long long> tmp = pq.top(); pq.pop();
long long du = tmp.first, u = tmp.second;
du = -du;
if (du != dist[u][source]) continue;
for (int v : adj[u]) if (dist[v][source] > max(h[v], dist[u][source] + 1)) {
dist[v][source] = max(h[v], dist[u][source] + 1);
pq.push(make_pair(-dist[v][source], v));
}
}
}
void process() {
// 0: from 1
// 1: from n
cin >> n >> m;
foru(i, 1, n) cin >> h[i];
foru(i, 1, m) {
int u, v; cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dijkstra(1, 0);
dijkstra(n, 1);
int ans = oo;
foru(u, 1, n) {
ans = min(ans, max(dist[u][0], dist[u][1]) * 2);
for (int v : adj[u]) {
ans = min(ans, max(dist[v][0], dist[u][1]) * 2 + 1);
ans = min(ans, max(dist[u][0], dist[v][1]) * 2 + 1);
}
}
cout << ans;
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# | 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... |