#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<vector<pair<int ,int>>> g;
signed main(){
int n ,m;
cin>>n>>m;
vector<int> a(n + 1);
for(int i =1;i<=n;i++)cin>>a[i];
g.resize(n+1);
vector<pair<int , int>> edge(m);
int ans=0;
for(int i =0;i<m;i++){
int u,v , c1 , c2;
cin>>u>>v;
if(u == n){
c1 = max(1LL , a[v]);
c2 = max(1LL ,a[v]);
g[u].push_back({v , c1});
g[v].push_back({u , c2});
continue;
}
if(v == n){
c1 = max(1LL , a[u]);
c2 = max(1LL ,a[u]);
g[u].push_back({v , c1});
g[v].push_back({u , c2});
continue;
}
if(u == 1){
c1 = max(1LL , a[v]);
c2 = max(1LL ,a[v]);
g[u].push_back({v , c1});
g[v].push_back({u , c2});
continue;
}
if(v == 1){
c1 = max(1LL , a[u]);
c2 = max(1LL ,a[u]);
g[u].push_back({v , c1});
g[v].push_back({u , c2});
continue;
}
if(a[u]> a[v]){
c1 = 1;
}
else{
c1 = max(1LL ,abs(a[u] - a[v]));
}
if(a[v] > a[u]){
c2 = 1;
}
else{
c2 = max(1LL , abs(a[u]-a[v]));
}
g[u].push_back({v , c1});
g[v].push_back({u , c2});
}
priority_queue<pair<int , int> , vector<pair<int , int>> , greater<pair<int , int>>> pq;
pq.push({0 , 1});
vector<int> d(n + 1 , LLONG_MAX);
d[1] =0;
while(!pq.empty()){
int dist = pq.top().first;
int u = pq.top().second;
pq.pop();
if(d[u] != dist) continue;
for(auto v : g[u]){
if(d[v.first] > dist + v.second){
d[v.first] = dist + v.second;
pq.push({d[v.first] , v.first});
// cout << v.first << "\n";
}
}
}
cout<<d[n];
}
| # | 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... |