/* author : bo may can 5 */
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define file(name) \
if(fopen(name".inp", "r")) \
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
const int MAX = 2e5 + 5;
int n, m, a[MAX], f[MAX], g[MAX];
vector <int> adj[MAX];
void dp(int s, int f[]) {
fill(f + 1, f + n + 1, INT_MAX);
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;
q.emplace(f[s] = a[s], s);
while(!q.empty()) {
auto [du, u] = q.top(); q.pop();
if(f[u] != du) continue;
for (auto v : adj[u]) if(f[v] > max(f[u] + 1, a[v])) {
f[v] = max(f[u] + 1, a[v]);
q.emplace(f[v], v);
}
}
}
void you_make_it(void) {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
while(m--) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dp(1, f), dp(n, g);
int ans = INT_MAX;
for (int i = 1; i <= n; ++i) ans = min(ans, max(f[i], g[i]) * 2);
// for (int i = 1; i <= n; ++i) cout << f[i] << " \n"[i == n];
// for (int i = 1; i <= n; ++i) cout << g[i] << " \n"[i == n];
for (int u = 1; u <= n; ++u) {
for (auto v : adj[u]) {
ans = min(ans, max(f[u], g[v]) * 2 + 1);
}
}
cout << ans;
}
signed main() {
#ifdef LOCAL
freopen("TASK.inp", "r", stdin);
freopen("TASK.out", "w", stdout);
#endif
file("airplane");
auto start_time = chrono::steady_clock::now();
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
you_make_it();
auto end_time = chrono::steady_clock::now();
cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;
return (0 ^ 0);
}
// Dream it. Wish it. Do it.
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:12:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:61:5: note: in expansion of macro 'file'
61 | file("airplane");
| ^~~~
Main.cpp:12:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:61:5: note: in expansion of macro 'file'
61 | file("airplane");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
57 ms |
15444 KB |
Output is correct |
3 |
Correct |
59 ms |
15740 KB |
Output is correct |
4 |
Correct |
60 ms |
15792 KB |
Output is correct |
5 |
Correct |
72 ms |
16016 KB |
Output is correct |
6 |
Correct |
60 ms |
15972 KB |
Output is correct |
7 |
Correct |
61 ms |
16040 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6752 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
3 ms |
7004 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
3 ms |
6808 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7000 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6752 KB |
Output is correct |
4 |
Correct |
2 ms |
7000 KB |
Output is correct |
5 |
Correct |
1 ms |
6744 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
3 ms |
7004 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
3 ms |
6744 KB |
Output is correct |
15 |
Correct |
3 ms |
6808 KB |
Output is correct |
16 |
Correct |
3 ms |
6748 KB |
Output is correct |
17 |
Correct |
3 ms |
6748 KB |
Output is correct |
18 |
Correct |
1 ms |
6792 KB |
Output is correct |
19 |
Correct |
3 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6748 KB |
Output is correct |
23 |
Correct |
3 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
4 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
3 ms |
6864 KB |
Output is correct |
28 |
Correct |
3 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
57 ms |
15444 KB |
Output is correct |
3 |
Correct |
59 ms |
15740 KB |
Output is correct |
4 |
Correct |
60 ms |
15792 KB |
Output is correct |
5 |
Correct |
72 ms |
16016 KB |
Output is correct |
6 |
Correct |
60 ms |
15972 KB |
Output is correct |
7 |
Correct |
61 ms |
16040 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
1 ms |
7000 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6752 KB |
Output is correct |
12 |
Correct |
2 ms |
7000 KB |
Output is correct |
13 |
Correct |
1 ms |
6744 KB |
Output is correct |
14 |
Correct |
2 ms |
6744 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
3 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
3 ms |
6744 KB |
Output is correct |
23 |
Correct |
3 ms |
6808 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6748 KB |
Output is correct |
26 |
Correct |
1 ms |
6792 KB |
Output is correct |
27 |
Correct |
3 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
31 |
Correct |
3 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
4 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
3 ms |
6864 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
6748 KB |
Output is correct |
39 |
Correct |
148 ms |
16108 KB |
Output is correct |
40 |
Correct |
79 ms |
15696 KB |
Output is correct |
41 |
Correct |
80 ms |
15696 KB |
Output is correct |
42 |
Correct |
83 ms |
15700 KB |
Output is correct |
43 |
Correct |
84 ms |
15848 KB |
Output is correct |
44 |
Correct |
82 ms |
15696 KB |
Output is correct |
45 |
Correct |
112 ms |
15696 KB |
Output is correct |
46 |
Correct |
85 ms |
15640 KB |
Output is correct |
47 |
Correct |
85 ms |
15696 KB |
Output is correct |
48 |
Correct |
116 ms |
15696 KB |
Output is correct |
49 |
Correct |
119 ms |
16132 KB |
Output is correct |
50 |
Correct |
115 ms |
16096 KB |
Output is correct |
51 |
Correct |
170 ms |
16092 KB |
Output is correct |
52 |
Correct |
172 ms |
18288 KB |
Output is correct |
53 |
Correct |
148 ms |
17488 KB |
Output is correct |
54 |
Correct |
178 ms |
17808 KB |
Output is correct |
55 |
Correct |
195 ms |
17740 KB |
Output is correct |
56 |
Correct |
239 ms |
20616 KB |
Output is correct |
57 |
Correct |
241 ms |
20416 KB |
Output is correct |
58 |
Correct |
258 ms |
20904 KB |
Output is correct |
59 |
Correct |
263 ms |
20960 KB |
Output is correct |
60 |
Correct |
209 ms |
21064 KB |
Output is correct |
61 |
Correct |
272 ms |
20696 KB |
Output is correct |
62 |
Correct |
212 ms |
20460 KB |
Output is correct |
63 |
Correct |
210 ms |
20396 KB |
Output is correct |
64 |
Correct |
180 ms |
20556 KB |
Output is correct |
65 |
Correct |
178 ms |
20388 KB |
Output is correct |
66 |
Correct |
228 ms |
20532 KB |
Output is correct |
67 |
Correct |
233 ms |
20620 KB |
Output is correct |