#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,M;
cin>>N>>M;
ll A[N];
ll maxA = 0;
for(ll i = 0;i < N;i++){
cin>>A[i];
maxA = max(maxA,A[i]);
}
vector<ll> v[N];
for(ll i = 0;i < M;i++){
ll a,b;
cin>>a>>b;
a--;
b--;
v[a].push_back(b);
v[b].push_back(a);
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > pq;
ll start[N]; //either 0 or N - 1
ll dist[N];
memset(start,-1,sizeof(start));
memset(dist,-1,sizeof(dist));
dist[0] = 0;
dist[N - 1] = 0;
start[0] = 0;
start[N - 1] = N - 1;
pq.push({dist[0],0});
pq.push({dist[N - 1],N - 1});
while(!pq.empty()){
ll x = pq.top().second;
ll d = pq.top().first;
pq.pop();
if(d != dist[x]){
continue;
}
for(auto u : v[x]){
if(dist[u] == -1 || dist[u] > max(A[u],dist[x] + 1)){
dist[u] = max(A[u],dist[x] + 1);
start[u] = start[x]; //started from node 0 or N - 1
pq.push({dist[u],u});
}
}
}
ll ans = 1e18;
for(ll x = 0;x < N;x++){
for(auto u : v[x]){
if(start[x] != start[u]){
//one started from node 0, another one from node N - 1
ll a = x;
ll b = u;
if(dist[a] > dist[b]) swap(a,b); //dist[a] is smaller
ans = min(ans,dist[b] + max(A[b],dist[a] + 1));
}
}
}
cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
57 ms |
19284 KB |
Output is correct |
3 |
Correct |
62 ms |
19684 KB |
Output is correct |
4 |
Correct |
60 ms |
19784 KB |
Output is correct |
5 |
Correct |
61 ms |
20304 KB |
Output is correct |
6 |
Correct |
67 ms |
20540 KB |
Output is correct |
7 |
Correct |
62 ms |
20172 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
580 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
580 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
57 ms |
19284 KB |
Output is correct |
3 |
Correct |
62 ms |
19684 KB |
Output is correct |
4 |
Correct |
60 ms |
19784 KB |
Output is correct |
5 |
Correct |
61 ms |
20304 KB |
Output is correct |
6 |
Correct |
67 ms |
20540 KB |
Output is correct |
7 |
Correct |
62 ms |
20172 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
580 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
504 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
720 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
536 KB |
Output is correct |
39 |
Correct |
119 ms |
21256 KB |
Output is correct |
40 |
Correct |
93 ms |
20576 KB |
Output is correct |
41 |
Correct |
83 ms |
20560 KB |
Output is correct |
42 |
Correct |
87 ms |
20504 KB |
Output is correct |
43 |
Correct |
84 ms |
20424 KB |
Output is correct |
44 |
Correct |
84 ms |
20564 KB |
Output is correct |
45 |
Correct |
86 ms |
20304 KB |
Output is correct |
46 |
Correct |
85 ms |
20524 KB |
Output is correct |
47 |
Correct |
86 ms |
20376 KB |
Output is correct |
48 |
Correct |
93 ms |
20524 KB |
Output is correct |
49 |
Correct |
115 ms |
21424 KB |
Output is correct |
50 |
Correct |
129 ms |
21080 KB |
Output is correct |
51 |
Correct |
109 ms |
21120 KB |
Output is correct |
52 |
Correct |
172 ms |
26592 KB |
Output is correct |
53 |
Correct |
147 ms |
25192 KB |
Output is correct |
54 |
Correct |
158 ms |
25292 KB |
Output is correct |
55 |
Correct |
171 ms |
25548 KB |
Output is correct |
56 |
Correct |
237 ms |
29892 KB |
Output is correct |
57 |
Correct |
215 ms |
30164 KB |
Output is correct |
58 |
Correct |
245 ms |
31296 KB |
Output is correct |
59 |
Correct |
208 ms |
31172 KB |
Output is correct |
60 |
Correct |
210 ms |
31088 KB |
Output is correct |
61 |
Correct |
218 ms |
30664 KB |
Output is correct |
62 |
Correct |
194 ms |
30272 KB |
Output is correct |
63 |
Correct |
192 ms |
30024 KB |
Output is correct |
64 |
Correct |
236 ms |
29928 KB |
Output is correct |
65 |
Correct |
192 ms |
30148 KB |
Output is correct |
66 |
Correct |
208 ms |
30152 KB |
Output is correct |
67 |
Correct |
219 ms |
30148 KB |
Output is correct |