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>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 2e5 + 1;
const string NAME = "";
int n, m, a[N];
long long d[2][N];
vector<int> adj[N];
void inp(){
cin >> n >> m;
for(int i = 1; i <= n; ++i)
cin >> a[i];
for(int i = 1; i <= m; ++i){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
}
void solve(){
priority_queue<pair<long long, int>> pq;
for(int i = 1; i <= n; ++i)
d[0][i] = d[1][i] = 1e16;
d[0][1] = d[1][n] = 0;
pq.push({0, 1});
while(!pq.empty()){
auto [dis, u] = pq.top();
pq.pop();
dis = -dis;
if(dis != d[0][u])
continue;
for(int v : adj[u]){
if(d[0][v] > max(d[0][u] + 1, 1LL * a[v])){
d[0][v] = max(d[0][u] + 1, 1LL * a[v]);
pq.push({-d[0][v], v});
}
}
}
pq.push({0, n});
while(!pq.empty()){
auto [dis, u] = pq.top();
pq.pop();
dis = -dis;
if(dis != d[1][u])
continue;
for(int v : adj[u]){
if(d[1][v] > max(d[1][u] + 1, 1LL * a[v])){
d[1][v] = max(d[1][u] + 1, 1LL * a[v]);
pq.push({-d[1][v], v});
}
}
}
long long ans = 1e16;
for(int u = 1; u <= n; ++u){
ans = min(ans, max(d[0][u], d[1][u]) * 2LL);
for(int v : adj[u])
ans = min(ans, max(d[0][u], d[1][v]) * 2LL + 1LL);
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:90:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen((NAME + ".out").c_str(), "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... |