#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
return l + rd() % (r - l + 1);
}
const int N = 1e6 + 5;
const int mod = 1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int inf = 1e9; // INT_MAX;
const long long llinf = 1e18; // LLONG_MAX;
template<class X, class Y>
bool minimize(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y>
bool maximize(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
template<class X, class Y>
bool add(X &a, Y b) {
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return true;
}
int n, m;
int h[N];
vector<int> adj[N];
vector<int> disS, disE;
void dijkstra(int s, vector<int> &dis) {
dis.assign(n + 5, inf);
vector<int> vis(n + 5, 0);
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
dis[s] = 0;
q.emplace(0, s);
while (q.size()) {
int du, u;
tie(du, u) = q.top(); q.pop();
// if (du > u) continue;
if (vis[u]) continue;
vis[u] = true;
for (int v : adj[u]) {
if (max(h[v], dis[u] + 1) < dis[v]) {
dis[v] = max(h[v], dis[u] + 1);
q.emplace(dis[v], v);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> h[i];
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dijkstra(1, disS);
dijkstra(n, disE);
int ans = inf;
for (int u = 1; u <= n; ++u) {
ans = min(ans, max(disS[u], disE[u]) * 2);
for (int v : adj[u]) {
ans = min(ans, max(disS[u], disE[v]) * 2 + 1);
ans = min(ans, max(disS[v], disE[u]) * 2 + 1);
}
}
cout << ans;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
61 ms |
37188 KB |
Output is correct |
3 |
Correct |
63 ms |
37436 KB |
Output is correct |
4 |
Correct |
64 ms |
37628 KB |
Output is correct |
5 |
Correct |
78 ms |
37968 KB |
Output is correct |
6 |
Correct |
66 ms |
37896 KB |
Output is correct |
7 |
Correct |
73 ms |
38124 KB |
Output is correct |
8 |
Correct |
6 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25176 KB |
Output is correct |
2 |
Correct |
5 ms |
25228 KB |
Output is correct |
3 |
Correct |
5 ms |
25176 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
6 ms |
25180 KB |
Output is correct |
6 |
Correct |
6 ms |
25180 KB |
Output is correct |
7 |
Correct |
8 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
8 ms |
25336 KB |
Output is correct |
10 |
Correct |
6 ms |
25180 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25180 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
7 ms |
25180 KB |
Output is correct |
15 |
Correct |
7 ms |
25228 KB |
Output is correct |
16 |
Correct |
7 ms |
25180 KB |
Output is correct |
17 |
Correct |
7 ms |
25232 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25176 KB |
Output is correct |
2 |
Correct |
5 ms |
25228 KB |
Output is correct |
3 |
Correct |
5 ms |
25176 KB |
Output is correct |
4 |
Correct |
5 ms |
25180 KB |
Output is correct |
5 |
Correct |
6 ms |
25180 KB |
Output is correct |
6 |
Correct |
6 ms |
25180 KB |
Output is correct |
7 |
Correct |
8 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
8 ms |
25336 KB |
Output is correct |
10 |
Correct |
6 ms |
25180 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25180 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
7 ms |
25180 KB |
Output is correct |
15 |
Correct |
7 ms |
25228 KB |
Output is correct |
16 |
Correct |
7 ms |
25180 KB |
Output is correct |
17 |
Correct |
7 ms |
25232 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
19 |
Correct |
7 ms |
25180 KB |
Output is correct |
20 |
Correct |
7 ms |
25272 KB |
Output is correct |
21 |
Correct |
6 ms |
25180 KB |
Output is correct |
22 |
Correct |
6 ms |
25396 KB |
Output is correct |
23 |
Correct |
7 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25180 KB |
Output is correct |
25 |
Correct |
6 ms |
25176 KB |
Output is correct |
26 |
Correct |
7 ms |
25180 KB |
Output is correct |
27 |
Correct |
7 ms |
25316 KB |
Output is correct |
28 |
Correct |
7 ms |
25436 KB |
Output is correct |
29 |
Correct |
7 ms |
25324 KB |
Output is correct |
30 |
Correct |
7 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
61 ms |
37188 KB |
Output is correct |
3 |
Correct |
63 ms |
37436 KB |
Output is correct |
4 |
Correct |
64 ms |
37628 KB |
Output is correct |
5 |
Correct |
78 ms |
37968 KB |
Output is correct |
6 |
Correct |
66 ms |
37896 KB |
Output is correct |
7 |
Correct |
73 ms |
38124 KB |
Output is correct |
8 |
Correct |
6 ms |
25176 KB |
Output is correct |
9 |
Correct |
6 ms |
25176 KB |
Output is correct |
10 |
Correct |
5 ms |
25228 KB |
Output is correct |
11 |
Correct |
5 ms |
25176 KB |
Output is correct |
12 |
Correct |
5 ms |
25180 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25180 KB |
Output is correct |
15 |
Correct |
8 ms |
25180 KB |
Output is correct |
16 |
Correct |
6 ms |
25180 KB |
Output is correct |
17 |
Correct |
8 ms |
25336 KB |
Output is correct |
18 |
Correct |
6 ms |
25180 KB |
Output is correct |
19 |
Correct |
6 ms |
25180 KB |
Output is correct |
20 |
Correct |
6 ms |
25180 KB |
Output is correct |
21 |
Correct |
6 ms |
25180 KB |
Output is correct |
22 |
Correct |
7 ms |
25180 KB |
Output is correct |
23 |
Correct |
7 ms |
25228 KB |
Output is correct |
24 |
Correct |
7 ms |
25180 KB |
Output is correct |
25 |
Correct |
7 ms |
25232 KB |
Output is correct |
26 |
Correct |
5 ms |
25180 KB |
Output is correct |
27 |
Correct |
7 ms |
25180 KB |
Output is correct |
28 |
Correct |
7 ms |
25272 KB |
Output is correct |
29 |
Correct |
6 ms |
25180 KB |
Output is correct |
30 |
Correct |
6 ms |
25396 KB |
Output is correct |
31 |
Correct |
7 ms |
25180 KB |
Output is correct |
32 |
Correct |
6 ms |
25180 KB |
Output is correct |
33 |
Correct |
6 ms |
25176 KB |
Output is correct |
34 |
Correct |
7 ms |
25180 KB |
Output is correct |
35 |
Correct |
7 ms |
25316 KB |
Output is correct |
36 |
Correct |
7 ms |
25436 KB |
Output is correct |
37 |
Correct |
7 ms |
25324 KB |
Output is correct |
38 |
Correct |
7 ms |
25436 KB |
Output is correct |
39 |
Correct |
161 ms |
38120 KB |
Output is correct |
40 |
Correct |
175 ms |
37312 KB |
Output is correct |
41 |
Correct |
115 ms |
37368 KB |
Output is correct |
42 |
Correct |
151 ms |
37308 KB |
Output is correct |
43 |
Correct |
112 ms |
37240 KB |
Output is correct |
44 |
Correct |
125 ms |
37240 KB |
Output is correct |
45 |
Correct |
112 ms |
37140 KB |
Output is correct |
46 |
Correct |
115 ms |
37188 KB |
Output is correct |
47 |
Correct |
108 ms |
37344 KB |
Output is correct |
48 |
Correct |
129 ms |
37572 KB |
Output is correct |
49 |
Correct |
189 ms |
38440 KB |
Output is correct |
50 |
Correct |
188 ms |
37896 KB |
Output is correct |
51 |
Correct |
187 ms |
38124 KB |
Output is correct |
52 |
Correct |
210 ms |
41020 KB |
Output is correct |
53 |
Correct |
205 ms |
39620 KB |
Output is correct |
54 |
Correct |
238 ms |
39848 KB |
Output is correct |
55 |
Correct |
275 ms |
39812 KB |
Output is correct |
56 |
Correct |
316 ms |
43136 KB |
Output is correct |
57 |
Correct |
324 ms |
43344 KB |
Output is correct |
58 |
Correct |
301 ms |
44184 KB |
Output is correct |
59 |
Correct |
309 ms |
44228 KB |
Output is correct |
60 |
Correct |
306 ms |
44096 KB |
Output is correct |
61 |
Correct |
328 ms |
44052 KB |
Output is correct |
62 |
Correct |
307 ms |
43084 KB |
Output is correct |
63 |
Correct |
220 ms |
42576 KB |
Output is correct |
64 |
Correct |
288 ms |
43048 KB |
Output is correct |
65 |
Correct |
295 ms |
43116 KB |
Output is correct |
66 |
Correct |
301 ms |
42592 KB |
Output is correct |
67 |
Correct |
267 ms |
43408 KB |
Output is correct |