#include <bits/stdc++.h>
#define lli long long int
#define MP make_pair
#define REP(i,n) for(int i = 0; (i)<(n); (i)++)
#define pb push_back
const int N = 2e5+5;
const int M = 4e3+5;
const int MOD = 1e9+7;
const int INF = 1e9 + 500;
using namespace std;
void fastio() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
int n,m,q;
vector<int> a(N, 0);
vector<vector<int> > adj(N, vector<int>());
vector<int> dp1, dp2;
void bfs(int src, vector<int> &sp) {
priority_queue<array<int,2>, vector<array<int,2> >, greater<array<int,2> > > que;
sp.assign(n + 3, INF);
sp[src] = 0;
que.push({0, src});
while(que.size()) {
auto cur = que.top();
que.pop();
for(auto c : adj[cur[1]]) {
if(sp[c] != INF) continue;
sp[c] = max(cur[0] + 1 , a[c]);
que.push({sp[c], c});
}
}
}
void solve() {
cin>>n>>m;
REP(i,n) cin>>a[i + 1];
for(int i = 1; i<=m; i++) {
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
int ans = INF;
bfs(1, dp1);
bfs(n, dp2);
for(int i = 1; i<=n; i++) {
int ret = max(dp1[i] , dp2[i]) * 2;
ans = min(ans , ret);
}
for(int i = 1; i<=n; i++) {
for(auto c : adj[i]) {
if(dp1[i] == dp2[c]) ans = min(ans, dp1[i] * 2 + 1);
}
}
cout<<ans<<"\n";
}
signed main() {
fastio();
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
57 ms |
16940 KB |
Output is correct |
3 |
Correct |
58 ms |
17236 KB |
Output is correct |
4 |
Correct |
59 ms |
17436 KB |
Output is correct |
5 |
Correct |
62 ms |
17888 KB |
Output is correct |
6 |
Correct |
61 ms |
17840 KB |
Output is correct |
7 |
Correct |
61 ms |
18000 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5920 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5964 KB |
Output is correct |
8 |
Correct |
3 ms |
6060 KB |
Output is correct |
9 |
Correct |
3 ms |
6044 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
3 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5980 KB |
Output is correct |
14 |
Correct |
3 ms |
5980 KB |
Output is correct |
15 |
Correct |
4 ms |
6040 KB |
Output is correct |
16 |
Correct |
4 ms |
5980 KB |
Output is correct |
17 |
Correct |
4 ms |
5980 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5724 KB |
Output is correct |
2 |
Correct |
3 ms |
5920 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5964 KB |
Output is correct |
8 |
Correct |
3 ms |
6060 KB |
Output is correct |
9 |
Correct |
3 ms |
6044 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
3 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5980 KB |
Output is correct |
14 |
Correct |
3 ms |
5980 KB |
Output is correct |
15 |
Correct |
4 ms |
6040 KB |
Output is correct |
16 |
Correct |
4 ms |
5980 KB |
Output is correct |
17 |
Correct |
4 ms |
5980 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5964 KB |
Output is correct |
21 |
Correct |
3 ms |
6020 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5980 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5988 KB |
Output is correct |
26 |
Correct |
4 ms |
5976 KB |
Output is correct |
27 |
Correct |
4 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
4 ms |
5976 KB |
Output is correct |
30 |
Correct |
4 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5720 KB |
Output is correct |
2 |
Correct |
57 ms |
16940 KB |
Output is correct |
3 |
Correct |
58 ms |
17236 KB |
Output is correct |
4 |
Correct |
59 ms |
17436 KB |
Output is correct |
5 |
Correct |
62 ms |
17888 KB |
Output is correct |
6 |
Correct |
61 ms |
17840 KB |
Output is correct |
7 |
Correct |
61 ms |
18000 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
2 ms |
5724 KB |
Output is correct |
10 |
Correct |
3 ms |
5920 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5724 KB |
Output is correct |
13 |
Correct |
2 ms |
5724 KB |
Output is correct |
14 |
Correct |
3 ms |
5980 KB |
Output is correct |
15 |
Correct |
3 ms |
5964 KB |
Output is correct |
16 |
Correct |
3 ms |
6060 KB |
Output is correct |
17 |
Correct |
3 ms |
6044 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
4 ms |
6040 KB |
Output is correct |
24 |
Correct |
4 ms |
5980 KB |
Output is correct |
25 |
Correct |
4 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
3 ms |
5964 KB |
Output is correct |
29 |
Correct |
3 ms |
6020 KB |
Output is correct |
30 |
Correct |
3 ms |
5980 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5980 KB |
Output is correct |
33 |
Correct |
3 ms |
5988 KB |
Output is correct |
34 |
Correct |
4 ms |
5976 KB |
Output is correct |
35 |
Correct |
4 ms |
5980 KB |
Output is correct |
36 |
Correct |
3 ms |
5980 KB |
Output is correct |
37 |
Correct |
4 ms |
5976 KB |
Output is correct |
38 |
Correct |
4 ms |
5980 KB |
Output is correct |
39 |
Correct |
127 ms |
18000 KB |
Output is correct |
40 |
Correct |
95 ms |
17300 KB |
Output is correct |
41 |
Correct |
92 ms |
17232 KB |
Output is correct |
42 |
Correct |
92 ms |
17232 KB |
Output is correct |
43 |
Correct |
91 ms |
17232 KB |
Output is correct |
44 |
Correct |
91 ms |
17232 KB |
Output is correct |
45 |
Correct |
133 ms |
16996 KB |
Output is correct |
46 |
Correct |
99 ms |
17236 KB |
Output is correct |
47 |
Correct |
100 ms |
17204 KB |
Output is correct |
48 |
Correct |
117 ms |
17088 KB |
Output is correct |
49 |
Correct |
127 ms |
18136 KB |
Output is correct |
50 |
Correct |
122 ms |
17744 KB |
Output is correct |
51 |
Correct |
107 ms |
17748 KB |
Output is correct |
52 |
Correct |
171 ms |
20732 KB |
Output is correct |
53 |
Correct |
154 ms |
19660 KB |
Output is correct |
54 |
Correct |
163 ms |
19716 KB |
Output is correct |
55 |
Correct |
190 ms |
19960 KB |
Output is correct |
56 |
Correct |
217 ms |
23096 KB |
Output is correct |
57 |
Correct |
217 ms |
23104 KB |
Output is correct |
58 |
Correct |
219 ms |
24228 KB |
Output is correct |
59 |
Correct |
211 ms |
24120 KB |
Output is correct |
60 |
Correct |
226 ms |
24136 KB |
Output is correct |
61 |
Correct |
243 ms |
23708 KB |
Output is correct |
62 |
Correct |
218 ms |
22844 KB |
Output is correct |
63 |
Correct |
206 ms |
23160 KB |
Output is correct |
64 |
Correct |
209 ms |
23112 KB |
Output is correct |
65 |
Correct |
237 ms |
23160 KB |
Output is correct |
66 |
Correct |
265 ms |
23160 KB |
Output is correct |
67 |
Correct |
212 ms |
23280 KB |
Output is correct |