This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T& x, T y) {x = max(x, y);}
template <typename T> inline void chkmin(T& x, T y) {x = min(x, y);}
template <typename T> inline void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
const int N = 2e5 + 10;
int n, m, a[N], f[2][N], ans = 2e9;
vector <int> v[N];
bool vis[N];
void dij(int t, int s) {
ms(vis, false);
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
q.emplace(f[t][s] = 0, s);
while (q.size()) {
int x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = true;
for (int i: v[x])
if (max(a[i], f[t][x] + 1) < f[t][i]) q.emplace(f[t][i] = max(a[i], f[t][x] + 1), i);
}
}
signed main() {
read(n), read(m);
F(i, 1, n) read(a[i]);
F(i, 1, m) {
int x, y; read(x), read(y);
v[x].push_back(y);
v[y].push_back(x);
}
ms(f, 0x3f);
dij(0, 1), dij(1, n);
F(i, 1, n) chkmin(ans, max(f[0][i], f[1][i]) * 2);
F(i, 1, n)
for (int j: v[i]) {
// if (f[0][i] < f[1][i]) chkmin(ans, f[1][i] * 2 + 1);
// else
chkmin(ans, max(f[0][i], f[1][j]) * 2 + 1);
}
cout << ans;
return 0;
}
/* why?
*/
# | 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... |