#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define int long long
int a[200005], f[200005], d[200005];
vector<int> g[200005];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
f[i] = d[i] = -1;
}
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
priority_queue<pair<int, int>> q;
q.push({0, 1});
f[1] = 0;
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int x = -p.first, u = p.second;
for (int w : g[u]) {
if (f[w] != -1) continue;
f[w] = max(x + 1, a[w]);
q.push({-f[w], w});
}
}
q.push({0, n});
d[n] = 0;
while (!q.empty()) {
pair<int, int> p = q.top();
q.pop();
int x = -p.first, u = p.second;
for (int w : g[u]) {
if (d[w] != -1) continue;
d[w] = max(x + 1, a[w]);
q.push({-d[w], w});
}
}
int res = 1e9;
for (int i = 1; i <= n; i++) {
res = min(res, max(f[i], d[i]) * 2);
}
for (int i = 1; i <= n; i++) {
for (int w : g[i]) {
if (f[i] == d[w]) res = min(res, f[i] * 2 + 1);
}
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
57 ms |
15900 KB |
Output is correct |
3 |
Correct |
59 ms |
15952 KB |
Output is correct |
4 |
Correct |
60 ms |
15960 KB |
Output is correct |
5 |
Correct |
61 ms |
15956 KB |
Output is correct |
6 |
Correct |
61 ms |
16092 KB |
Output is correct |
7 |
Correct |
60 ms |
15952 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8860 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8848 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
8876 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8860 KB |
Output is correct |
7 |
Correct |
2 ms |
8792 KB |
Output is correct |
8 |
Correct |
3 ms |
8796 KB |
Output is correct |
9 |
Correct |
4 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8848 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
9052 KB |
Output is correct |
16 |
Correct |
3 ms |
9052 KB |
Output is correct |
17 |
Correct |
3 ms |
8876 KB |
Output is correct |
18 |
Correct |
1 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
3 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8796 KB |
Output is correct |
24 |
Correct |
3 ms |
8796 KB |
Output is correct |
25 |
Correct |
3 ms |
8884 KB |
Output is correct |
26 |
Correct |
3 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8996 KB |
Output is correct |
29 |
Correct |
3 ms |
8852 KB |
Output is correct |
30 |
Correct |
3 ms |
8852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
57 ms |
15900 KB |
Output is correct |
3 |
Correct |
59 ms |
15952 KB |
Output is correct |
4 |
Correct |
60 ms |
15960 KB |
Output is correct |
5 |
Correct |
61 ms |
15956 KB |
Output is correct |
6 |
Correct |
61 ms |
16092 KB |
Output is correct |
7 |
Correct |
60 ms |
15952 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8860 KB |
Output is correct |
15 |
Correct |
2 ms |
8792 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
4 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8848 KB |
Output is correct |
22 |
Correct |
3 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
9052 KB |
Output is correct |
24 |
Correct |
3 ms |
9052 KB |
Output is correct |
25 |
Correct |
3 ms |
8876 KB |
Output is correct |
26 |
Correct |
1 ms |
8796 KB |
Output is correct |
27 |
Correct |
2 ms |
8796 KB |
Output is correct |
28 |
Correct |
3 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
30 |
Correct |
2 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
3 ms |
8796 KB |
Output is correct |
33 |
Correct |
3 ms |
8884 KB |
Output is correct |
34 |
Correct |
3 ms |
8796 KB |
Output is correct |
35 |
Correct |
3 ms |
8796 KB |
Output is correct |
36 |
Correct |
3 ms |
8996 KB |
Output is correct |
37 |
Correct |
3 ms |
8852 KB |
Output is correct |
38 |
Correct |
3 ms |
8852 KB |
Output is correct |
39 |
Correct |
130 ms |
21264 KB |
Output is correct |
40 |
Correct |
93 ms |
20560 KB |
Output is correct |
41 |
Correct |
95 ms |
20656 KB |
Output is correct |
42 |
Correct |
101 ms |
20560 KB |
Output is correct |
43 |
Correct |
113 ms |
20612 KB |
Output is correct |
44 |
Correct |
91 ms |
20528 KB |
Output is correct |
45 |
Correct |
96 ms |
20304 KB |
Output is correct |
46 |
Correct |
112 ms |
20364 KB |
Output is correct |
47 |
Correct |
123 ms |
20584 KB |
Output is correct |
48 |
Correct |
97 ms |
20512 KB |
Output is correct |
49 |
Correct |
132 ms |
21332 KB |
Output is correct |
50 |
Correct |
165 ms |
21120 KB |
Output is correct |
51 |
Correct |
133 ms |
21328 KB |
Output is correct |
52 |
Correct |
195 ms |
26520 KB |
Output is correct |
53 |
Correct |
176 ms |
25200 KB |
Output is correct |
54 |
Correct |
174 ms |
25296 KB |
Output is correct |
55 |
Correct |
195 ms |
25460 KB |
Output is correct |
56 |
Correct |
239 ms |
29992 KB |
Output is correct |
57 |
Correct |
276 ms |
30152 KB |
Output is correct |
58 |
Correct |
241 ms |
31280 KB |
Output is correct |
59 |
Correct |
342 ms |
31048 KB |
Output is correct |
60 |
Correct |
247 ms |
30924 KB |
Output is correct |
61 |
Correct |
246 ms |
30816 KB |
Output is correct |
62 |
Correct |
246 ms |
30152 KB |
Output is correct |
63 |
Correct |
221 ms |
29900 KB |
Output is correct |
64 |
Correct |
232 ms |
29896 KB |
Output is correct |
65 |
Correct |
262 ms |
30036 KB |
Output is correct |
66 |
Correct |
246 ms |
30152 KB |
Output is correct |
67 |
Correct |
240 ms |
30180 KB |
Output is correct |