#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<bool> vb;
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back
#define sp <<" "<<
#define cont(x) for(auto el:x) cout<<el<<' ';cout<<endl;
#define contp(x) for(auto el:x) cout<<el.fi<<'-'<<el.se<<' ';cout<<endl;
#define DEBUG(x) cout<<#x sp x<<endl;
#define carp(x,y) ((x%MOD)*(y%MOD))%MOD
#define topla(x,y) ((x%MOD)+(y%MOD))%MOD
#define mid (l+r)/2
const int MAXN=1e6+5;
const int MOD=1e9+7;
const int INF=1e18;
int n,m;
vi a;
vi adj[MAXN];
int artir[MAXN], azalt[MAXN];
int cur[MAXN];
int d[MAXN];
void dij(){
set<pii> s;
s.insert({0,1});
d[1]=0;
cur[1]=0;
FORE(i,2,n+1) d[i]=INF;
while(!s.empty()){
auto el=*s.begin();
s.erase(el);
int nd=el.se;
if(nd==n){
cout<<d[nd]+cur[nd]-azalt[nd]<<endl;
break;
}
for(auto kom:adj[nd]){
int cost=1;
if(a[kom]>cur[nd]){
cost=a[kom]-cur[nd]-artir[nd];
}
if(d[kom]>d[nd]+cost){
if(d[kom]!=INF) s.erase({d[kom],kom});
d[kom]=d[nd]+cost;
if(a[kom]>cur[nd]){
cur[kom]=a[kom];
azalt[kom]=0;
}
else if(a[kom]<cur[nd]){
cur[kom]=cur[nd];
azalt[kom]=azalt[nd]+1;
artir[kom]=artir[nd]+1;
}
else{
cur[kom]=a[kom];
azalt[kom]=0;
artir[kom]=artir[nd]+1;
}
s.insert({d[kom],kom});
}
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
a.resize(n+1);
FORE(i,1,n+1) cin>>a[i];
FOR(i,m){
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dij();
}
# | 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... |