思路:
二分+spfa;
二分最大费用,然后判断只走小于等于二分答案的点是否可以花费小于体力上限的血量;
来,上代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 50050 #define maxm 500050 #define ll long long #define INF 0x7fffffff ll head[maxn],cnt,dis[maxn],que[maxm<<4]; ll n,m,hp,cost[maxn],E[maxm],V[maxm],W[maxm]; bool if_[maxn]; inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } inline void edge_add() { ll u,v,w;in(u),in(v),in(w); E[++cnt]=head[u],head[u]=cnt,V[cnt]=v,W[cnt]=w; E[++cnt]=head[v],head[v]=cnt,V[cnt]=u,W[cnt]=w; } inline bool spfa(ll lit) { if(lit<cost[1]) return false; for(ll i=1;i<=n;i++) dis[i]=INF,if_[i]=false; dis[1]=0,if_[1]=true,que[0]=1;int h=0,tail=1; while(h<tail) { ll now=que[h++];if_[now]=false; for(ll i=head[now];i;i=E[i]) { if(cost[V[i]]<=lit&&dis[V[i]]>dis[now]+W[i]) { dis[V[i]]=dis[now]+W[i]; if(!if_[V[i]]) { if_[V[i]]=true; que[tail++]=V[i]; } } } } return dis[n]<=hp; } int main() { in(n),in(m),in(hp);ll l=INF,r=0,ans=0; for(ll i=1;i<=n;i++) in(cost[i]),l=min(l,cost[i]),r=max(r,cost[i]); while(m--) edge_add(); while(l<=r) { ll mid=l+r>>1; if(spfa(mid)) ans=mid,r=mid-1; else l=mid+1; } if(ans==0) printf("AFK"); else cout<<ans; return 0; }