#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 |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
59 ms |
36920 KB |
Output is correct |
3 |
Correct |
63 ms |
37444 KB |
Output is correct |
4 |
Correct |
63 ms |
37484 KB |
Output is correct |
5 |
Correct |
63 ms |
37888 KB |
Output is correct |
6 |
Correct |
63 ms |
37892 KB |
Output is correct |
7 |
Correct |
66 ms |
37956 KB |
Output is correct |
8 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25432 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25220 KB |
Output is correct |
4 |
Correct |
5 ms |
25228 KB |
Output is correct |
5 |
Correct |
7 ms |
25472 KB |
Output is correct |
6 |
Correct |
6 ms |
25180 KB |
Output is correct |
7 |
Correct |
6 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
7 ms |
25276 KB |
Output is correct |
10 |
Correct |
6 ms |
25176 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25312 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25180 KB |
Output is correct |
15 |
Correct |
6 ms |
25436 KB |
Output is correct |
16 |
Correct |
6 ms |
25180 KB |
Output is correct |
17 |
Correct |
6 ms |
25436 KB |
Output is correct |
18 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25432 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
3 |
Correct |
5 ms |
25220 KB |
Output is correct |
4 |
Correct |
5 ms |
25228 KB |
Output is correct |
5 |
Correct |
7 ms |
25472 KB |
Output is correct |
6 |
Correct |
6 ms |
25180 KB |
Output is correct |
7 |
Correct |
6 ms |
25180 KB |
Output is correct |
8 |
Correct |
6 ms |
25180 KB |
Output is correct |
9 |
Correct |
7 ms |
25276 KB |
Output is correct |
10 |
Correct |
6 ms |
25176 KB |
Output is correct |
11 |
Correct |
6 ms |
25180 KB |
Output is correct |
12 |
Correct |
6 ms |
25312 KB |
Output is correct |
13 |
Correct |
6 ms |
25180 KB |
Output is correct |
14 |
Correct |
6 ms |
25180 KB |
Output is correct |
15 |
Correct |
6 ms |
25436 KB |
Output is correct |
16 |
Correct |
6 ms |
25180 KB |
Output is correct |
17 |
Correct |
6 ms |
25436 KB |
Output is correct |
18 |
Correct |
5 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 |
6 ms |
25180 KB |
Output is correct |
23 |
Correct |
6 ms |
25180 KB |
Output is correct |
24 |
Correct |
6 ms |
25188 KB |
Output is correct |
25 |
Correct |
6 ms |
25180 KB |
Output is correct |
26 |
Correct |
6 ms |
25180 KB |
Output is correct |
27 |
Correct |
6 ms |
25180 KB |
Output is correct |
28 |
Correct |
9 ms |
25432 KB |
Output is correct |
29 |
Correct |
6 ms |
25320 KB |
Output is correct |
30 |
Correct |
6 ms |
25436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
2 |
Correct |
59 ms |
36920 KB |
Output is correct |
3 |
Correct |
63 ms |
37444 KB |
Output is correct |
4 |
Correct |
63 ms |
37484 KB |
Output is correct |
5 |
Correct |
63 ms |
37888 KB |
Output is correct |
6 |
Correct |
63 ms |
37892 KB |
Output is correct |
7 |
Correct |
66 ms |
37956 KB |
Output is correct |
8 |
Correct |
5 ms |
25180 KB |
Output is correct |
9 |
Correct |
5 ms |
25432 KB |
Output is correct |
10 |
Correct |
5 ms |
25180 KB |
Output is correct |
11 |
Correct |
5 ms |
25220 KB |
Output is correct |
12 |
Correct |
5 ms |
25228 KB |
Output is correct |
13 |
Correct |
7 ms |
25472 KB |
Output is correct |
14 |
Correct |
6 ms |
25180 KB |
Output is correct |
15 |
Correct |
6 ms |
25180 KB |
Output is correct |
16 |
Correct |
6 ms |
25180 KB |
Output is correct |
17 |
Correct |
7 ms |
25276 KB |
Output is correct |
18 |
Correct |
6 ms |
25176 KB |
Output is correct |
19 |
Correct |
6 ms |
25180 KB |
Output is correct |
20 |
Correct |
6 ms |
25312 KB |
Output is correct |
21 |
Correct |
6 ms |
25180 KB |
Output is correct |
22 |
Correct |
6 ms |
25180 KB |
Output is correct |
23 |
Correct |
6 ms |
25436 KB |
Output is correct |
24 |
Correct |
6 ms |
25180 KB |
Output is correct |
25 |
Correct |
6 ms |
25436 KB |
Output is correct |
26 |
Correct |
5 ms |
25180 KB |
Output is correct |
27 |
Correct |
6 ms |
25180 KB |
Output is correct |
28 |
Correct |
6 ms |
25180 KB |
Output is correct |
29 |
Correct |
6 ms |
25180 KB |
Output is correct |
30 |
Correct |
6 ms |
25180 KB |
Output is correct |
31 |
Correct |
6 ms |
25180 KB |
Output is correct |
32 |
Correct |
6 ms |
25188 KB |
Output is correct |
33 |
Correct |
6 ms |
25180 KB |
Output is correct |
34 |
Correct |
6 ms |
25180 KB |
Output is correct |
35 |
Correct |
6 ms |
25180 KB |
Output is correct |
36 |
Correct |
9 ms |
25432 KB |
Output is correct |
37 |
Correct |
6 ms |
25320 KB |
Output is correct |
38 |
Correct |
6 ms |
25436 KB |
Output is correct |
39 |
Correct |
139 ms |
37956 KB |
Output is correct |
40 |
Correct |
89 ms |
37388 KB |
Output is correct |
41 |
Correct |
91 ms |
37184 KB |
Output is correct |
42 |
Correct |
90 ms |
37172 KB |
Output is correct |
43 |
Correct |
89 ms |
37260 KB |
Output is correct |
44 |
Correct |
93 ms |
37364 KB |
Output is correct |
45 |
Correct |
99 ms |
37124 KB |
Output is correct |
46 |
Correct |
98 ms |
37180 KB |
Output is correct |
47 |
Correct |
94 ms |
37312 KB |
Output is correct |
48 |
Correct |
91 ms |
37188 KB |
Output is correct |
49 |
Correct |
121 ms |
38152 KB |
Output is correct |
50 |
Correct |
115 ms |
37956 KB |
Output is correct |
51 |
Correct |
108 ms |
37988 KB |
Output is correct |
52 |
Correct |
171 ms |
40892 KB |
Output is correct |
53 |
Correct |
151 ms |
39668 KB |
Output is correct |
54 |
Correct |
152 ms |
39700 KB |
Output is correct |
55 |
Correct |
173 ms |
39952 KB |
Output is correct |
56 |
Correct |
227 ms |
43404 KB |
Output is correct |
57 |
Correct |
231 ms |
43316 KB |
Output is correct |
58 |
Correct |
226 ms |
44340 KB |
Output is correct |
59 |
Correct |
231 ms |
44504 KB |
Output is correct |
60 |
Correct |
203 ms |
44100 KB |
Output is correct |
61 |
Correct |
225 ms |
43972 KB |
Output is correct |
62 |
Correct |
245 ms |
43376 KB |
Output is correct |
63 |
Correct |
189 ms |
43204 KB |
Output is correct |
64 |
Correct |
199 ms |
43080 KB |
Output is correct |
65 |
Correct |
194 ms |
43080 KB |
Output is correct |
66 |
Correct |
213 ms |
43244 KB |
Output is correct |
67 |
Correct |
214 ms |
43484 KB |
Output is correct |