#include <bits/stdc++.h>
using namespace std;
const int maxN = 4e5 + 5;
const int INF = 1e9;
int N, M, a[maxN];
vector<int> adj[maxN];
pair<int, int> edge[maxN];
int f[maxN], g[maxN];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Q;
void dijsktra(int s, int d[]){
for(int i = 1; i <= N; ++i)d[i] = INF;
d[s] = 0;
Q.push({0, s});
while(!Q.empty()){
int dist = Q.top().first;
int u = Q.top().second;
Q.pop();
if(dist > d[u])continue;
for(int v : adj[u]){
int w = max(a[v], dist + 1);
if(w < d[v]){
d[v] = w;
Q.push({w, v});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N >> M;
for(int i = 1; i <= N; ++i){
cin >> a[i];
}
for(int i = 1, u, v; i <= M; ++i){
cin >> u >> v ;
adj[u].push_back(v);
adj[v].push_back(u);
edge[i] = make_pair(u, v);
}
dijsktra(1, f);
dijsktra(N, g);
int res = INF;
for(int i = 1; i <= N; ++i){
res = min(res, f[i] + g[i] + abs(f[i] - g[i]));
}
for(int i = 1; i <= M; ++i){
int u = edge[i].first, v = edge[i].second;
if(f[u] == g[v])res = min(res, f[u] + g[v] + 1);
if(f[v] == g[u])res = min(res, f[v] + g[u] + 1);
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
59 ms |
23184 KB |
Output is correct |
3 |
Correct |
56 ms |
23304 KB |
Output is correct |
4 |
Correct |
52 ms |
23112 KB |
Output is correct |
5 |
Correct |
53 ms |
23224 KB |
Output is correct |
6 |
Correct |
58 ms |
23112 KB |
Output is correct |
7 |
Correct |
54 ms |
23112 KB |
Output is correct |
8 |
Correct |
3 ms |
16720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
3 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16824 KB |
Output is correct |
5 |
Correct |
2 ms |
16720 KB |
Output is correct |
6 |
Correct |
4 ms |
16936 KB |
Output is correct |
7 |
Correct |
3 ms |
16720 KB |
Output is correct |
8 |
Correct |
3 ms |
16720 KB |
Output is correct |
9 |
Correct |
3 ms |
16720 KB |
Output is correct |
10 |
Correct |
3 ms |
16720 KB |
Output is correct |
11 |
Correct |
3 ms |
16720 KB |
Output is correct |
12 |
Correct |
3 ms |
16720 KB |
Output is correct |
13 |
Correct |
4 ms |
16720 KB |
Output is correct |
14 |
Correct |
4 ms |
17144 KB |
Output is correct |
15 |
Correct |
4 ms |
16976 KB |
Output is correct |
16 |
Correct |
4 ms |
16976 KB |
Output is correct |
17 |
Correct |
4 ms |
16976 KB |
Output is correct |
18 |
Correct |
3 ms |
16720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
3 ms |
16720 KB |
Output is correct |
3 |
Correct |
3 ms |
16720 KB |
Output is correct |
4 |
Correct |
3 ms |
16824 KB |
Output is correct |
5 |
Correct |
2 ms |
16720 KB |
Output is correct |
6 |
Correct |
4 ms |
16936 KB |
Output is correct |
7 |
Correct |
3 ms |
16720 KB |
Output is correct |
8 |
Correct |
3 ms |
16720 KB |
Output is correct |
9 |
Correct |
3 ms |
16720 KB |
Output is correct |
10 |
Correct |
3 ms |
16720 KB |
Output is correct |
11 |
Correct |
3 ms |
16720 KB |
Output is correct |
12 |
Correct |
3 ms |
16720 KB |
Output is correct |
13 |
Correct |
4 ms |
16720 KB |
Output is correct |
14 |
Correct |
4 ms |
17144 KB |
Output is correct |
15 |
Correct |
4 ms |
16976 KB |
Output is correct |
16 |
Correct |
4 ms |
16976 KB |
Output is correct |
17 |
Correct |
4 ms |
16976 KB |
Output is correct |
18 |
Correct |
3 ms |
16720 KB |
Output is correct |
19 |
Correct |
3 ms |
16720 KB |
Output is correct |
20 |
Correct |
3 ms |
16720 KB |
Output is correct |
21 |
Correct |
3 ms |
16928 KB |
Output is correct |
22 |
Correct |
4 ms |
16720 KB |
Output is correct |
23 |
Correct |
5 ms |
16720 KB |
Output is correct |
24 |
Correct |
4 ms |
16720 KB |
Output is correct |
25 |
Correct |
4 ms |
16720 KB |
Output is correct |
26 |
Correct |
4 ms |
16976 KB |
Output is correct |
27 |
Correct |
4 ms |
16976 KB |
Output is correct |
28 |
Correct |
4 ms |
16976 KB |
Output is correct |
29 |
Correct |
4 ms |
16976 KB |
Output is correct |
30 |
Correct |
4 ms |
16976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16720 KB |
Output is correct |
2 |
Correct |
59 ms |
23184 KB |
Output is correct |
3 |
Correct |
56 ms |
23304 KB |
Output is correct |
4 |
Correct |
52 ms |
23112 KB |
Output is correct |
5 |
Correct |
53 ms |
23224 KB |
Output is correct |
6 |
Correct |
58 ms |
23112 KB |
Output is correct |
7 |
Correct |
54 ms |
23112 KB |
Output is correct |
8 |
Correct |
3 ms |
16720 KB |
Output is correct |
9 |
Correct |
3 ms |
16720 KB |
Output is correct |
10 |
Correct |
3 ms |
16720 KB |
Output is correct |
11 |
Correct |
3 ms |
16720 KB |
Output is correct |
12 |
Correct |
3 ms |
16824 KB |
Output is correct |
13 |
Correct |
2 ms |
16720 KB |
Output is correct |
14 |
Correct |
4 ms |
16936 KB |
Output is correct |
15 |
Correct |
3 ms |
16720 KB |
Output is correct |
16 |
Correct |
3 ms |
16720 KB |
Output is correct |
17 |
Correct |
3 ms |
16720 KB |
Output is correct |
18 |
Correct |
3 ms |
16720 KB |
Output is correct |
19 |
Correct |
3 ms |
16720 KB |
Output is correct |
20 |
Correct |
3 ms |
16720 KB |
Output is correct |
21 |
Correct |
4 ms |
16720 KB |
Output is correct |
22 |
Correct |
4 ms |
17144 KB |
Output is correct |
23 |
Correct |
4 ms |
16976 KB |
Output is correct |
24 |
Correct |
4 ms |
16976 KB |
Output is correct |
25 |
Correct |
4 ms |
16976 KB |
Output is correct |
26 |
Correct |
3 ms |
16720 KB |
Output is correct |
27 |
Correct |
3 ms |
16720 KB |
Output is correct |
28 |
Correct |
3 ms |
16720 KB |
Output is correct |
29 |
Correct |
3 ms |
16928 KB |
Output is correct |
30 |
Correct |
4 ms |
16720 KB |
Output is correct |
31 |
Correct |
5 ms |
16720 KB |
Output is correct |
32 |
Correct |
4 ms |
16720 KB |
Output is correct |
33 |
Correct |
4 ms |
16720 KB |
Output is correct |
34 |
Correct |
4 ms |
16976 KB |
Output is correct |
35 |
Correct |
4 ms |
16976 KB |
Output is correct |
36 |
Correct |
4 ms |
16976 KB |
Output is correct |
37 |
Correct |
4 ms |
16976 KB |
Output is correct |
38 |
Correct |
4 ms |
16976 KB |
Output is correct |
39 |
Correct |
91 ms |
23120 KB |
Output is correct |
40 |
Correct |
75 ms |
23348 KB |
Output is correct |
41 |
Correct |
90 ms |
23116 KB |
Output is correct |
42 |
Correct |
83 ms |
23112 KB |
Output is correct |
43 |
Correct |
92 ms |
23376 KB |
Output is correct |
44 |
Correct |
83 ms |
23164 KB |
Output is correct |
45 |
Correct |
86 ms |
23112 KB |
Output is correct |
46 |
Correct |
83 ms |
23268 KB |
Output is correct |
47 |
Correct |
80 ms |
23112 KB |
Output is correct |
48 |
Correct |
79 ms |
23368 KB |
Output is correct |
49 |
Correct |
96 ms |
23368 KB |
Output is correct |
50 |
Correct |
104 ms |
23268 KB |
Output is correct |
51 |
Correct |
98 ms |
23268 KB |
Output is correct |
52 |
Correct |
170 ms |
24328 KB |
Output is correct |
53 |
Correct |
129 ms |
24404 KB |
Output is correct |
54 |
Correct |
129 ms |
24396 KB |
Output is correct |
55 |
Correct |
148 ms |
24516 KB |
Output is correct |
56 |
Correct |
189 ms |
26420 KB |
Output is correct |
57 |
Correct |
216 ms |
26232 KB |
Output is correct |
58 |
Correct |
213 ms |
26200 KB |
Output is correct |
59 |
Correct |
190 ms |
26304 KB |
Output is correct |
60 |
Correct |
175 ms |
26304 KB |
Output is correct |
61 |
Correct |
178 ms |
26304 KB |
Output is correct |
62 |
Correct |
178 ms |
26304 KB |
Output is correct |
63 |
Correct |
176 ms |
26328 KB |
Output is correct |
64 |
Correct |
235 ms |
26304 KB |
Output is correct |
65 |
Correct |
240 ms |
26264 KB |
Output is correct |
66 |
Correct |
223 ms |
26204 KB |
Output is correct |
67 |
Correct |
272 ms |
26304 KB |
Output is correct |