#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define pb push_back
#define endl "\n"
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int h[n];
for(int &g : h) cin >> g;
vector<pii> adj[n+1];
for(int i=0; i<m; ++i) {
int x, y;
cin >> x >> y;
int w=abs(h[x-1]-h[y-1]);
if(!w) w=1;
adj[x].pb({y,w});
adj[y].pb({x,w});
}
int dist[n+1];
const int inf=0x3f3f3f3f;
memset(dist,inf,n+1);
dist[1]=0;
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({0,1});
while(!pq.empty()) {
auto [d,at] = pq.top();
pq.pop();
if(d>dist[at]) continue;
for(auto &[to,w] : adj[at]) {
if((d+w)<dist[to]) {
dist[to] = d+w;
pq.push({d+w,to});
}
}
}
cout << dist[n] << endl;
}
# | 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... |