/*
Em la may troi trong gio
Anh la cay nhieu dan do
*/
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const int mod = 1e9 + 7;
const signed N = 2e5 + 5;
const int oo = 1e9;
int n, m;
vector<int> adj[N];
pair<int, int> t[N << 1];
int d1[N], dn[N], h[N];
priority_queue<pii, vector<pii>, greater<pii>> q;
void output() {
for (int i = 1; i <= n; ++i) {
d1[i] = oo;
dn[i] = oo;
}
d1[1] = 0;
q.push(make_pair(1, d1[1]));
while(!q.empty()) {
auto[u, c] = q.top();
q.pop();
if (d1[u] != c) continue;
for (int v : adj[u]) {
if (d1[v] > max(h[v], d1[u] + 1)) {
d1[v] = max(h[v], d1[u] + 1);
q.push(make_pair(v, d1[v]));
}
}
}
while(!q.empty()) q.pop();
dn[n] = 0;
q.push(make_pair(n, dn[n]));
while(!q.empty()) {
auto[u, c] = q.top();
q.pop();
if (dn[u] != c) continue;
for (int v : adj[u]) {
if (dn[v] > max(h[v], dn[u] + 1)) {
dn[v] = max(h[v], dn[u] + 1);
q.push(make_pair(v, dn[v]));
}
}
}
int ans = oo;
for (int i = 1; i <= n; ++i) ans = min(ans, (max(d1[i], dn[i]) << 1));
for (int i = 1; i <= m; ++i) {
auto [u, v] = t[i];
ans = min(ans, ((max(dn[u], d1[v]) << 1) | 1));
ans = min(ans, ((max(dn[v], d1[u]) << 1) | 1));
}
cout << ans;
return;
}
void input() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> h[i];
for (int i = 1; i <= m; ++i) {
cin >> t[i].fi >> t[i].se;
int u = t[i].fi, v = t[i].se;
adj[u].pb(v);
adj[v].pb(u);
}
output();
return;
}
signed main() {
if(fopen("", "r")) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
input();
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# | 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... |