D-松鼠回家_2023河南萌新联赛第(一)场:河南农业大学 (nowcoder.com)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
map<int,int>a;
int n,m,st,ed,h;
struct node{
int x,y;
};
vector<node>hh[N];
int dis[N],vis[N];
struct cmp{
bool operator()(const node &a,const node &b){
return a.y<b.y;
}
};
void dij(int k){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<node,vector<node>,cmp >q;
q.push({st,0});
dis[st]=0;
while(q.size()){
node now=q.top();
q.pop();
int st=now.x;
if(a[st]>k)continue;
if(vis[st])continue;
vis[st]=1;
for(auto it:hh[st]){
if(a[it.x]>k)continue;
if(dis[it.x]>dis[st]+it.y){
dis[it.x]=dis[st]+it.y;
q.push({it.x,dis[it.x]});
}
}
}
}
bool check(int mid){
dij(mid);
if(dis[ed]-dis[st]<=h)return 1;
else return 0;
}
signed main(){
cin>>n>>m>>st>>ed>>h;
for(int i=1;i<=n;i++){
cin>>a[i];
}
while(m--){
int x,y,z;
cin>>x>>y>>z;
hh[x].push_back({y,z});
hh[y].push_back({x,z});
}
int l=max(a[st],a[ed]),r=1e7+10;
while(l<=r){
int mid=(l+r)/2;
if(check(mid))r=mid-1;
else l=mid+1;
}
if(check(l)==0)cout<<"-1"<<endl;
else cout<<l<<endl;
return 0;
}