#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int> Dijkstra(int start, vector<vector<int>>& adj, vector<int>& a){
int n = (int)adj.size();
vector<int> dist(n, 1e18);
vector<bool> vis(n, false);
dist[start] = 0;
priority_queue<pair<int, int>> q; q.push({0, start});
while(!q.empty()){
int x = q.top().second; q.pop();
if(vis[x]) continue;
vis[x] = true;
for(int& node : adj[x]){
if(dist[node] > max(a[node], dist[x] + 1)){
dist[node] = max(a[node], dist[x] + 1);
q.push({-dist[node], node});
}
}
}
return dist;
}
void solve(){
int n, m; cin >> n >> m;
vector<int> a(n);
for(auto &i : a) cin >> i;
vector<vector<int>> adj(n);
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v; u--; v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
vector<int> dist1 = Dijkstra(0, adj, a);
vector<int> distN = Dijkstra(n-1, adj, a);
int ans = 1e18;
for(int i = 0; i < n; i++){
int cost = max(dist1[i], distN[i]) * 2;
if(abs(dist1[i] - distN[i]) == 1) cost--;
ans = min(ans, cost);
}
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// int tt; cin >> tt;
int tt = 1;
while(tt--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
60 ms |
15956 KB |
Output is correct |
3 |
Correct |
60 ms |
15952 KB |
Output is correct |
4 |
Correct |
63 ms |
15952 KB |
Output is correct |
5 |
Correct |
64 ms |
15956 KB |
Output is correct |
6 |
Correct |
63 ms |
16092 KB |
Output is correct |
7 |
Correct |
64 ms |
16112 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
444 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
800 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
444 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
800 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
600 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
60 ms |
15956 KB |
Output is correct |
3 |
Correct |
60 ms |
15952 KB |
Output is correct |
4 |
Correct |
63 ms |
15952 KB |
Output is correct |
5 |
Correct |
64 ms |
15956 KB |
Output is correct |
6 |
Correct |
63 ms |
16092 KB |
Output is correct |
7 |
Correct |
64 ms |
16112 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
444 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
4 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
800 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
508 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
600 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
171 ms |
21300 KB |
Output is correct |
40 |
Correct |
111 ms |
20524 KB |
Output is correct |
41 |
Correct |
119 ms |
20620 KB |
Output is correct |
42 |
Correct |
108 ms |
20560 KB |
Output is correct |
43 |
Correct |
127 ms |
20536 KB |
Output is correct |
44 |
Correct |
103 ms |
20560 KB |
Output is correct |
45 |
Correct |
125 ms |
20280 KB |
Output is correct |
46 |
Correct |
110 ms |
20424 KB |
Output is correct |
47 |
Correct |
119 ms |
20576 KB |
Output is correct |
48 |
Correct |
103 ms |
20560 KB |
Output is correct |
49 |
Correct |
160 ms |
21328 KB |
Output is correct |
50 |
Correct |
164 ms |
21256 KB |
Output is correct |
51 |
Correct |
135 ms |
21072 KB |
Output is correct |
52 |
Correct |
225 ms |
27236 KB |
Output is correct |
53 |
Correct |
232 ms |
26268 KB |
Output is correct |
54 |
Correct |
223 ms |
26128 KB |
Output is correct |
55 |
Correct |
253 ms |
26144 KB |
Output is correct |
56 |
Correct |
304 ms |
31264 KB |
Output is correct |
57 |
Correct |
403 ms |
31524 KB |
Output is correct |
58 |
Correct |
347 ms |
32336 KB |
Output is correct |
59 |
Correct |
346 ms |
32340 KB |
Output is correct |
60 |
Correct |
363 ms |
32224 KB |
Output is correct |
61 |
Correct |
304 ms |
32012 KB |
Output is correct |
62 |
Correct |
297 ms |
31020 KB |
Output is correct |
63 |
Correct |
475 ms |
31448 KB |
Output is correct |
64 |
Correct |
319 ms |
31280 KB |
Output is correct |
65 |
Correct |
291 ms |
31240 KB |
Output is correct |
66 |
Correct |
398 ms |
31364 KB |
Output is correct |
67 |
Correct |
400 ms |
31524 KB |
Output is correct |