#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B>
string to_string(pair<A, B>);
string to_string(string S) {
return '"' + S + '"';
}
string to_string(const char* c) {
return to_string(string(c));
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template<size_t T>
string to_string(bitset<T> bs) {
return bs.to_string();
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < int(v.size()); ++i) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(v[i]);
}
return res + "}";
}
template<typename T>
string to_string(T v) {
string res = "{";
for (auto e : v) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(e);
}
return res + "}";
}
template<typename A, typename B>
string to_string(pair<A, B> p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
void debug_out() {
cerr << endl;
}
template<typename H, typename... T>
void debug_out(H head, T... tail) {
cerr << " " << to_string(head);
debug_out(tail...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif
template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<int>> g(N);
for (int i = 0; i < M; ++i) {
int X, Y;
cin >> X >> Y;
--X, --Y;
g[X].push_back(Y);
g[Y].push_back(X);
}
auto Sp = [&](int source) {
vector<int> d(N, -1);
min_pq<pair<int, int>> pq;
auto Add = [&](int x, int nd) {
nd = max(nd, A[x]);
if (d[x] == -1 || nd < d[x]) {
d[x] = nd;
pq.emplace(nd, x);
}
};
Add(source, 0);
while (!pq.empty()) {
auto[dist, v] = pq.top();
pq.pop();
if (d[v] < dist) {
continue;
}
for (auto u : g[v]) {
Add(u, dist + 1);
}
}
return d;
};
auto d_s = Sp(0);
auto d_t = Sp(N - 1);
int ans = int(1e9);
for (int i = 0; i < N; ++i) {
ans = min(ans, max(d_s[i], d_t[i]) * 2);
for (auto u : g[i]) {
if (d_s[i] == d_t[u]) {
ans = min(ans, d_s[i] * 2 + 1);
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
54 ms |
13756 KB |
Output is correct |
3 |
Correct |
56 ms |
13652 KB |
Output is correct |
4 |
Correct |
56 ms |
13568 KB |
Output is correct |
5 |
Correct |
58 ms |
13648 KB |
Output is correct |
6 |
Correct |
72 ms |
13648 KB |
Output is correct |
7 |
Correct |
58 ms |
13652 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
604 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 |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
572 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
604 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 |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
444 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
2 ms |
604 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 |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
54 ms |
13756 KB |
Output is correct |
3 |
Correct |
56 ms |
13652 KB |
Output is correct |
4 |
Correct |
56 ms |
13568 KB |
Output is correct |
5 |
Correct |
58 ms |
13648 KB |
Output is correct |
6 |
Correct |
72 ms |
13648 KB |
Output is correct |
7 |
Correct |
58 ms |
13652 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
572 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
604 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 |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
444 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
2 ms |
604 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 |
600 KB |
Output is correct |
39 |
Correct |
135 ms |
15956 KB |
Output is correct |
40 |
Correct |
85 ms |
15692 KB |
Output is correct |
41 |
Correct |
154 ms |
16076 KB |
Output is correct |
42 |
Correct |
85 ms |
15700 KB |
Output is correct |
43 |
Correct |
86 ms |
15700 KB |
Output is correct |
44 |
Correct |
84 ms |
15764 KB |
Output is correct |
45 |
Correct |
107 ms |
15684 KB |
Output is correct |
46 |
Correct |
121 ms |
15884 KB |
Output is correct |
47 |
Correct |
87 ms |
15696 KB |
Output is correct |
48 |
Correct |
98 ms |
15816 KB |
Output is correct |
49 |
Correct |
164 ms |
16044 KB |
Output is correct |
50 |
Correct |
101 ms |
15956 KB |
Output is correct |
51 |
Correct |
109 ms |
15948 KB |
Output is correct |
52 |
Correct |
186 ms |
18456 KB |
Output is correct |
53 |
Correct |
220 ms |
17576 KB |
Output is correct |
54 |
Correct |
180 ms |
17944 KB |
Output is correct |
55 |
Correct |
193 ms |
17916 KB |
Output is correct |
56 |
Correct |
233 ms |
20824 KB |
Output is correct |
57 |
Correct |
293 ms |
20544 KB |
Output is correct |
58 |
Correct |
248 ms |
21112 KB |
Output is correct |
59 |
Correct |
275 ms |
20948 KB |
Output is correct |
60 |
Correct |
222 ms |
20944 KB |
Output is correct |
61 |
Correct |
320 ms |
20636 KB |
Output is correct |
62 |
Correct |
246 ms |
20568 KB |
Output is correct |
63 |
Correct |
300 ms |
20612 KB |
Output is correct |
64 |
Correct |
279 ms |
20652 KB |
Output is correct |
65 |
Correct |
264 ms |
20488 KB |
Output is correct |
66 |
Correct |
352 ms |
20556 KB |
Output is correct |
67 |
Correct |
323 ms |
20540 KB |
Output is correct |