/*
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 d[N][2], h[N];
void dijkstra(int s, bool type) {
for (int i = 1; i <= n; ++i) d[i][type] = oo;
priority_queue<pii, vector<pii>, greater<pii>> q;
d[s][type] = 0;
q.push(make_pair(s, d[s][type]));
while(!q.empty()) {
auto[u, c] = q.top();
q.pop();
if (d[u][type] != c) continue;
for (int v : adj[u]) {
if (d[v][type] > max(h[v], d[u][type] + 1)) {
d[v][type] = max(h[v], d[u][type] + 1);
q.push(make_pair(v, d[v][type]));
}
}
}
}
void output() {
dijkstra(1, 0);
dijkstra(n, 1);
int ans = oo;
for (int i = 1; i <= n; ++i) ans = min(ans, (max(d[i][0], d[i][1]) << 1));
for (int i = 1; i <= m; ++i) {
auto [u, v] = t[i];
ans = min(ans, ((max(d[u][0], d[v][1]) << 1) | 1));
ans = min(ans, ((max(d[v][0], d[u][1]) << 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:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | 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... |