#include <bits/stdc++.h>
#define int long long
#define double long double
#define pii pair <int,int>
#define tiii tuple <int, int, int>
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define ub(a, b) upper_bound(a.begin(), a.end(), b) - a.begin()
#define lb(a, b) lower_bound(a.begin(), a.end(), b) - a.begin()
#define ve vector
#define graph(a, n) vector <int> a[n];
#define wgraph(a, n) vector <pii> a[n];
#define emb emplace_back
#define em emplace
#define ins insert
#define er erase
#define iShowSpeed cin.tie(NULL)->sync_with_stdio(false)
using namespace std;
template <typename T>
using greater_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1e9 + 7;
const int inf = 1e18;
const int N = 2e5 + 5;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n, m, a[N];
vector <int> adj[N];
vector <int> bfs(int st){
queue <pii> q; q.emplace(0, st);
vector <int> dis(n + 1, inf); dis[st] = 0;
while (!q.empty()) {
auto [w, u] = q.front(); q.pop();
if (w > dis[u]) continue;
for (auto v : adj[u]) {
if (w + 1 >= dis[v]) continue;
dis[v] = w + 1;
q.emplace(dis[v], v);
}
}
return dis;
}
int32_t main(){
iShowSpeed;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
adj[u].emb(v);
adj[v].emb(u);
}
vector <int> dis1 = bfs(1);
vector <int> dis2 = bfs(n);
int l = 1, r = inf, ans = inf;
while (l <= r) {
int mid = (l + r) / 2;
vector <bool> ok(n + 1);
for (int i = 1; i <= n; i++) {
if (max(dis1[i], a[i]) + max(dis2[i], a[i]) <= mid) ok[i] = true;
}
vector <bool> visited(n + 1);
queue <int> q; if (ok[1]) q.emplace(1), visited[1] = true;
while (!q.empty()) {
auto u = q.front(); q.pop();
for (auto v : adj[u]) {
if (!visited[v] && ok[v]) {
visited[v] = true;
q.emplace(v);
}
}
}
if (visited[n]) {
r = mid - 1;
ans = min(ans, mid);
}
else l = mid + 1;
}
cout << ans;
}
# | 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... |