// Programmer: Shadow1
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using str = string; // yay python!
#define i64 int64_t
#define show(x) cerr << (#x) << " = " << (x) << '\n';
#define output_vector(v) for(auto &x : v){cout << x << ' ';}cout << '\n';
#define output_pairvector(v) for(auto &x : v){cout << x.first << " " << x.second << '\n';}
#define read_vector(v) for(auto &x : v){cin >> x;}
#define vt vector
#define prq priority_queue
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define fir first
#define sec second
#define sz(x) ll(x.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
//
//
const int INF = 1e18;
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n+1), adj[n+1], d1(n+1, INF), d2(n+1, INF), h(n+1);
vector<bool> vis(n+1);
for(int i=1; i<=n; ++i)
cin >> a[i];
for(int i=0; i<m; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(n <= 2) {
cout << 0 << '\n';
return;
}
priority_queue<pii, vector<pii>, greater<pii>> pq;
d1[1] = 0;
pq.push({0, 1});
while(!pq.empty()) {
int u = pq.top().sec;
pq.pop();
if(vis[u]) continue;
vis[u] = true;
for(auto &v : adj[u]) {
if(d1[u] + max(1ll, a[v] - h[u]) < d1[v]) {
d1[v] = d1[u] + max(1ll, a[v] - h[u]);
h[v] = max(a[v], h[u] + 1);
pq.push({d1[v] - h[v], v});
}
}
}
while(!pq.empty()) pq.pop();
for(int i=1; i<=n; ++i) {
vis[i] = false;
h[i] = 0;
}
d2[n] = 0;
pq.push({0, n});
while(!pq.empty()) {
int u = pq.top().sec;
pq.pop();
if(vis[u]) continue;
vis[u] = true;
for(auto &v : adj[u]) {
if(d2[u] + max(1ll, a[v] - h[u]) < d2[v]) {
d2[v] = d2[u] + max(1ll, a[v] - h[u]);
h[v] = max(a[v], h[u] + 1);
pq.push({d2[v] - h[v], v});
}
}
}
int ans = INF;
for(int i=2; i<n; ++i) {
ans = min(ans, d1[i] + d2[i]);
}
cout << ans << '\n';
}
// CHECK YOUR OVERFLOWS!!!!
signed main() {
// freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin >> T;
for(int tc = 1; tc <= T; ++tc) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:104:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |