P1462 通往奥格瑞玛的道路

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<LL,int> PII;
const int N=1e4+10,M=1e5+10;

int h[N],ne[M],idx,e[M],w[M];
int n,m;
LL b;
LL f[N];
LL dist[N];
bool st[N];

void add(int a,int b,int c)
{
    e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}

bool dijkstra(LL limit)
{

    if(f[1]>limit||f[n]>limit) return false;
    
    memset(st,0,sizeof st);
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;

    priority_queue<PII,vector<PII>,greater<PII>> heap;
    heap.push({dist[1],1});

    while(!heap.empty()){
        auto t=heap.top();
        heap.pop();

        int ver=t.second;

        if(st[ver]) continue;
        st[ver]=true;

        for(int i=h[ver];i!=-1;i=ne[i]){
            int j=e[i];
            if(f[j]>limit) continue;
            if(dist[j]>dist[ver]+w[i]){
                dist[j]=dist[ver]+w[i];
                heap.push({dist[j],j});
            }
        }
    }

    return dist[n]<=b;
}

int main()
{

    memset(h,-1,sizeof h);
    
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>m>>b;

    for(int i=1;i<=n;i++) cin>>f[i];

    for(int i=1;i<=m;i++){
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c),add(b,a,c);
    }

    LL max_fee=0;
    for(int i=1;i<=n;i++) max_fee=max(max_fee,f[i]);

    if(!dijkstra(max_fee+1)){
         cout<<"AFK"<<endl;
         return 0;
    }

    int l=0,r=max_fee;

    LL ans=b;
    
    while(l<=r){
        int mid=l+(r-l)/2;
        if(dijkstra(mid)){
            r=mid-1;
            ans=mid;
        }else{
            l=mid+1;
        }
    }

    cout<<ans<<endl;
    
}
posted @ 2026-01-25 20:50  AnoSky  阅读(0)  评论(0)    收藏  举报