Submission #1184959

#TimeUsernameProblemLanguageResultExecution timeMemory
1184959NotLinuxAirplane (NOI23_airplane)C++20
100 / 100
499 ms25160 KiB
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
const int inf = 1e9 + 7;
void solve(){
    int n,m;
    cin >> n >> m;
    int a[n];
    for(int i = 0;i<n;i++)
    	cin >> a[i];
   	vector<int>graph[n];
   	for(int i = 0;i<m;i++){
   		int u,v;
   		cin >> u >> v;
   		u-- , v--;
   		graph[u].push_back(v);
   		graph[v].push_back(u);
   	}
   	int dist1[n] , height1[n] , vis1[n] = {0};
   	priority_queue<array<int,3>>bfs;// -dist , node , height
   	bfs.push({0,0,0});
   	while(!bfs.empty()){
   		auto tmp = bfs.top();
   		bfs.pop();
   		if(vis1[tmp[1]])continue;
   		vis1[tmp[1]] = 1;
   		dist1[tmp[1]] = -tmp[0];
   		height1[tmp[1]] = tmp[2];
   		for(auto itr : graph[tmp[1]]){
   			bfs.push({tmp[0] - (max(tmp[2]+1,a[itr]) - tmp[2]) , itr , max(tmp[2]+1,a[itr])});
   		}
   	}
   	int dist2[n] , height2[n] , vis2[n] = {0};
   	while(!bfs.empty())bfs.pop();
   	bfs.push({0,n-1,0});
   	while(!bfs.empty()){
   		auto tmp = bfs.top();
   		bfs.pop();
   		if(vis2[tmp[1]])continue;
   		vis2[tmp[1]] = 1;
   		dist2[tmp[1]] = -tmp[0];
   		height2[tmp[1]] = tmp[2];
   		for(auto itr : graph[tmp[1]]){
   			bfs.push({tmp[0] - (max(tmp[2]+1,a[itr]) - tmp[2]) , itr , max(tmp[2]+1,a[itr])});
   		}
   	}
   	int ans = inf;
   	for(int i = 0;i<n;i++){
   		// node max
   		// cout << i+1 << " : " << dist1[i] + dist2[i] + abs(height1[i] - height2[i]) << endl;
   		ans = min(ans , dist1[i] + dist2[i] + abs(height1[i] - height2[i]));
   		// edge max
   		for(auto itr : graph[i]){
   			// cout << i+1 << " " << itr+1 << " : " << dist1[i] + dist2[itr] + abs(height1[i] - height2[itr]) << endl;
   			ans = min(ans , dist1[i] + dist2[itr] + abs(height1[i] - height2[itr]) + 1);
   		}
   	}
   	cout << ans << endl;
}
signed main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int testcase=1;//cin >> testcase;
	while(testcase--)solve();
	cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...