This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |