#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#define maxn 10001
#define maxm 50001
using namespace std;
int n,m,c,val[maxn];
long long b;
inline int read(){
register int x(0),f(1); register char c(getchar());
while(c<'0'||'9'<c){ if(c=='-') f=-1; c=getchar(); }
while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
struct edge{
int u,v,w;
}e[maxm];
vector<int> to[maxn],w[maxn];
long long dis[maxn];
int ans=-1;
bool vis[maxn];
priority_queue< pair<int,int>,vector< pair<int,int> >,greater< pair<int,int> > > q;
inline bool dijkstra(){
memset(dis,0x3f,sizeof dis),memset(vis,false,sizeof vis);
q.push(make_pair(dis[1]=0,1));
while(q.size()){
int u=q.top().second; q.pop();
if(vis[u]) continue; vis[u]=true;
for(register int i=0;i<to[u].size();i++){
int v=to[u][i];
if(dis[v]>dis[u]+w[u][i]) dis[v]=dis[u]+w[u][i],q.push(make_pair(dis[v],v));
}
}
return dis[n]<=b;
}
inline void binary(){
int l=0,r=c,mid;
while(l<=r){
mid=l+r>>1;
for(register int i=1;i<=n;i++) to[i].clear(),w[i].clear();
for(register int i=1;i<=m;i++) if(val[e[i].u]<=mid&&val[e[i].v]<=mid){
int u=e[i].u,v=e[i].v;
to[u].push_back(v),w[u].push_back(e[i].w);
to[v].push_back(u),w[v].push_back(e[i].w);
}
if(dijkstra()) ans=mid,r=mid-1;
else l=mid+1;
}
}
int main(){
n=read(),m=read(),scanf("%lld",&b);
for(register int i=1;i<=n;i++) val[i]=read(),c=max(c,val[i]);
for(register int i=1;i<=m;i++) e[i].u=read(),e[i].v=read(),e[i].w=read();
binary();
if(ans==-1) puts("AFK");
else printf("%lld\n",ans);
return 0;
}
原文:https://www.cnblogs.com/akura/p/11066818.html