// 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 |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
35 ms |
13856 KB |
Output is correct |
3 |
Correct |
37 ms |
13908 KB |
Output is correct |
4 |
Correct |
36 ms |
13916 KB |
Output is correct |
5 |
Correct |
37 ms |
13908 KB |
Output is correct |
6 |
Correct |
43 ms |
13904 KB |
Output is correct |
7 |
Correct |
37 ms |
13908 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6828 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
7004 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7000 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6828 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
3 ms |
7004 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
7000 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
7004 KB |
Output is correct |
28 |
Correct |
3 ms |
7160 KB |
Output is correct |
29 |
Correct |
3 ms |
6868 KB |
Output is correct |
30 |
Correct |
3 ms |
7004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6748 KB |
Output is correct |
2 |
Correct |
35 ms |
13856 KB |
Output is correct |
3 |
Correct |
37 ms |
13908 KB |
Output is correct |
4 |
Correct |
36 ms |
13916 KB |
Output is correct |
5 |
Correct |
37 ms |
13908 KB |
Output is correct |
6 |
Correct |
43 ms |
13904 KB |
Output is correct |
7 |
Correct |
37 ms |
13908 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6748 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6828 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6744 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
7004 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
7000 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
3 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
7004 KB |
Output is correct |
36 |
Correct |
3 ms |
7160 KB |
Output is correct |
37 |
Correct |
3 ms |
6868 KB |
Output is correct |
38 |
Correct |
3 ms |
7004 KB |
Output is correct |
39 |
Correct |
91 ms |
18008 KB |
Output is correct |
40 |
Correct |
67 ms |
17236 KB |
Output is correct |
41 |
Correct |
69 ms |
17444 KB |
Output is correct |
42 |
Correct |
71 ms |
17236 KB |
Output is correct |
43 |
Correct |
68 ms |
17244 KB |
Output is correct |
44 |
Correct |
66 ms |
17424 KB |
Output is correct |
45 |
Correct |
67 ms |
17140 KB |
Output is correct |
46 |
Correct |
80 ms |
17424 KB |
Output is correct |
47 |
Correct |
68 ms |
17236 KB |
Output is correct |
48 |
Correct |
69 ms |
17232 KB |
Output is correct |
49 |
Correct |
93 ms |
18252 KB |
Output is correct |
50 |
Correct |
95 ms |
18068 KB |
Output is correct |
51 |
Correct |
86 ms |
18052 KB |
Output is correct |
52 |
Correct |
149 ms |
20984 KB |
Output is correct |
53 |
Correct |
135 ms |
19712 KB |
Output is correct |
54 |
Correct |
124 ms |
19896 KB |
Output is correct |
55 |
Correct |
140 ms |
19960 KB |
Output is correct |
56 |
Correct |
171 ms |
23176 KB |
Output is correct |
57 |
Correct |
232 ms |
23336 KB |
Output is correct |
58 |
Correct |
241 ms |
24432 KB |
Output is correct |
59 |
Correct |
206 ms |
24384 KB |
Output is correct |
60 |
Correct |
160 ms |
24076 KB |
Output is correct |
61 |
Correct |
193 ms |
23924 KB |
Output is correct |
62 |
Correct |
167 ms |
23052 KB |
Output is correct |
63 |
Correct |
150 ms |
23124 KB |
Output is correct |
64 |
Correct |
234 ms |
23152 KB |
Output is correct |
65 |
Correct |
172 ms |
23124 KB |
Output is correct |
66 |
Correct |
175 ms |
23112 KB |
Output is correct |
67 |
Correct |
208 ms |
23272 KB |
Output is correct |