#include <bits/stdc++.h>
#define int long long
#define re(a, b, c, d) for (auto a = b; a <= c; a += d)
#define de(a, b, c, d) for (auto a = b; a >= c; a -= d)
#define ms (a); memset (a, 0, sizeof a);
#define imax INT_MAX
#define imin INT_MIN
#define wh(a) while (a --)
#define PII pair <int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
template <typename T> bool chkmin (T& a, T b) {
return (b < a) ? a = b, 1 : 0;
}
template <typename T> bool chkmax (T& a, T b) {
return (b > a) ? a = b, 1 : 0;
}
using namespace std;
const int N = 5e5 + 5;
int T, n, m, a[N], d[3][N], ans = 1e18;
vector <int> g[N];
void dij (int t, int s) {
re (i, 1, n, 1) d[t][i] = 1e18;
d[t][s] = 0;
priority_queue <PII, vector <PII>, greater<PII> >pq;
pq.push ({d[t][s], s});
while (! pq.empty ()) {
int x = pq.top ().S;
pq.pop();
for (int v : g[x]) if (d[t][v] > max (d[t][x] + 1, a[v])) {
d[t][v] = max (d[t][x] + 1, a[v]);
pq.push ({d[t][v], v});
}
}
}
signed main () {
cout << fixed << setprecision (0);
ios :: sync_with_stdio (0), cin.tie (0), cout.tie (0);
cin >> n >> m;
int x, y;
re (i, 1, n, 1) cin >> a[i];
re (i, 1, m, 1) cin >> x >> y, g[x].pb (y), g[y].pb (x);
dij (0, 1);
dij (1, n);
re (i, 1, n, 1) if (abs (d[0][i] - d[1][i]) <= 1) chkmin (ans, d[0][i] + d[1][i]);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
60 ms |
32596 KB |
Output is correct |
3 |
Correct |
60 ms |
32944 KB |
Output is correct |
4 |
Correct |
61 ms |
33012 KB |
Output is correct |
5 |
Correct |
62 ms |
33620 KB |
Output is correct |
6 |
Correct |
63 ms |
33676 KB |
Output is correct |
7 |
Correct |
62 ms |
33616 KB |
Output is correct |
8 |
Correct |
4 ms |
16984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
17064 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
5 ms |
16984 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
6 ms |
16988 KB |
Output is correct |
11 |
Correct |
5 ms |
16984 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
5 ms |
16984 KB |
Output is correct |
15 |
Correct |
6 ms |
17240 KB |
Output is correct |
16 |
Correct |
7 ms |
17264 KB |
Output is correct |
17 |
Correct |
6 ms |
17244 KB |
Output is correct |
18 |
Correct |
4 ms |
17048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
4 ms |
16988 KB |
Output is correct |
3 |
Correct |
4 ms |
16988 KB |
Output is correct |
4 |
Correct |
4 ms |
16988 KB |
Output is correct |
5 |
Correct |
4 ms |
17064 KB |
Output is correct |
6 |
Correct |
4 ms |
16988 KB |
Output is correct |
7 |
Correct |
4 ms |
16988 KB |
Output is correct |
8 |
Correct |
5 ms |
16984 KB |
Output is correct |
9 |
Correct |
4 ms |
16988 KB |
Output is correct |
10 |
Correct |
6 ms |
16988 KB |
Output is correct |
11 |
Correct |
5 ms |
16984 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
5 ms |
16984 KB |
Output is correct |
15 |
Correct |
6 ms |
17240 KB |
Output is correct |
16 |
Correct |
7 ms |
17264 KB |
Output is correct |
17 |
Correct |
6 ms |
17244 KB |
Output is correct |
18 |
Correct |
4 ms |
17048 KB |
Output is correct |
19 |
Correct |
4 ms |
16984 KB |
Output is correct |
20 |
Correct |
4 ms |
16984 KB |
Output is correct |
21 |
Correct |
4 ms |
17240 KB |
Output is correct |
22 |
Correct |
5 ms |
16988 KB |
Output is correct |
23 |
Correct |
4 ms |
17084 KB |
Output is correct |
24 |
Correct |
4 ms |
16984 KB |
Output is correct |
25 |
Correct |
4 ms |
16988 KB |
Output is correct |
26 |
Correct |
4 ms |
16988 KB |
Output is correct |
27 |
Correct |
5 ms |
17244 KB |
Output is correct |
28 |
Correct |
5 ms |
17244 KB |
Output is correct |
29 |
Correct |
5 ms |
17244 KB |
Output is correct |
30 |
Correct |
5 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
16988 KB |
Output is correct |
2 |
Correct |
60 ms |
32596 KB |
Output is correct |
3 |
Correct |
60 ms |
32944 KB |
Output is correct |
4 |
Correct |
61 ms |
33012 KB |
Output is correct |
5 |
Correct |
62 ms |
33620 KB |
Output is correct |
6 |
Correct |
63 ms |
33676 KB |
Output is correct |
7 |
Correct |
62 ms |
33616 KB |
Output is correct |
8 |
Correct |
4 ms |
16984 KB |
Output is correct |
9 |
Correct |
3 ms |
16988 KB |
Output is correct |
10 |
Correct |
4 ms |
16988 KB |
Output is correct |
11 |
Correct |
4 ms |
16988 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
17064 KB |
Output is correct |
14 |
Correct |
4 ms |
16988 KB |
Output is correct |
15 |
Correct |
4 ms |
16988 KB |
Output is correct |
16 |
Correct |
5 ms |
16984 KB |
Output is correct |
17 |
Correct |
4 ms |
16988 KB |
Output is correct |
18 |
Correct |
6 ms |
16988 KB |
Output is correct |
19 |
Correct |
5 ms |
16984 KB |
Output is correct |
20 |
Correct |
4 ms |
16988 KB |
Output is correct |
21 |
Correct |
4 ms |
16988 KB |
Output is correct |
22 |
Correct |
5 ms |
16984 KB |
Output is correct |
23 |
Correct |
6 ms |
17240 KB |
Output is correct |
24 |
Correct |
7 ms |
17264 KB |
Output is correct |
25 |
Correct |
6 ms |
17244 KB |
Output is correct |
26 |
Correct |
4 ms |
17048 KB |
Output is correct |
27 |
Correct |
4 ms |
16984 KB |
Output is correct |
28 |
Correct |
4 ms |
16984 KB |
Output is correct |
29 |
Correct |
4 ms |
17240 KB |
Output is correct |
30 |
Correct |
5 ms |
16988 KB |
Output is correct |
31 |
Correct |
4 ms |
17084 KB |
Output is correct |
32 |
Correct |
4 ms |
16984 KB |
Output is correct |
33 |
Correct |
4 ms |
16988 KB |
Output is correct |
34 |
Correct |
4 ms |
16988 KB |
Output is correct |
35 |
Correct |
5 ms |
17244 KB |
Output is correct |
36 |
Correct |
5 ms |
17244 KB |
Output is correct |
37 |
Correct |
5 ms |
17244 KB |
Output is correct |
38 |
Correct |
5 ms |
17244 KB |
Output is correct |
39 |
Correct |
166 ms |
34644 KB |
Output is correct |
40 |
Correct |
92 ms |
33876 KB |
Output is correct |
41 |
Correct |
97 ms |
33872 KB |
Output is correct |
42 |
Correct |
94 ms |
33952 KB |
Output is correct |
43 |
Correct |
98 ms |
33876 KB |
Output is correct |
44 |
Correct |
107 ms |
33876 KB |
Output is correct |
45 |
Correct |
123 ms |
33736 KB |
Output is correct |
46 |
Correct |
99 ms |
33872 KB |
Output is correct |
47 |
Correct |
101 ms |
33948 KB |
Output is correct |
48 |
Correct |
98 ms |
33876 KB |
Output is correct |
49 |
Correct |
139 ms |
35080 KB |
Output is correct |
50 |
Correct |
140 ms |
34500 KB |
Output is correct |
51 |
Correct |
125 ms |
34384 KB |
Output is correct |
52 |
Correct |
283 ms |
40652 KB |
Output is correct |
53 |
Correct |
191 ms |
39804 KB |
Output is correct |
54 |
Correct |
185 ms |
39268 KB |
Output is correct |
55 |
Correct |
245 ms |
39476 KB |
Output is correct |
56 |
Correct |
284 ms |
44692 KB |
Output is correct |
57 |
Correct |
246 ms |
44748 KB |
Output is correct |
58 |
Correct |
273 ms |
45600 KB |
Output is correct |
59 |
Correct |
266 ms |
45708 KB |
Output is correct |
60 |
Correct |
231 ms |
45516 KB |
Output is correct |
61 |
Correct |
326 ms |
45280 KB |
Output is correct |
62 |
Correct |
217 ms |
44508 KB |
Output is correct |
63 |
Correct |
221 ms |
44392 KB |
Output is correct |
64 |
Correct |
229 ms |
44444 KB |
Output is correct |
65 |
Correct |
255 ms |
44492 KB |
Output is correct |
66 |
Correct |
256 ms |
44704 KB |
Output is correct |
67 |
Correct |
297 ms |
44808 KB |
Output is correct |