# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1102374 | tuannm | Airplane (NOI23_airplane) | C++17 | 49 ms | 15184 KiB |
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});
}
}
}
if(n < 3)
return (void) (cout << n - 1);
long long ans = 1e16;
for(int i = 2; i < n; ++i)
ans = min(ans, d[0][i] + d[1][i]);
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)
# | 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... |