wa#5#8,Dijkstra+优先队列的

P1462 通往奥格瑞玛的道路

SpringQinHao @ 2024-06-23 19:06:55

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
const int N=2e5;
typedef long long ll;
using namespace std;
struct edge{
    ll u,to;
    ll w;
    edge(ll a,ll b,ll c)
    {
        u=a,to=b,w=c;
    }
};
vector <edge> e[N];
struct node
{
    ll id,dis;
    node(ll a,ll b){
        id=a;dis=b;
    }
};
struct cmp
{   
    bool operator()(node a,node b)
    {
        return a.dis>b.dis;
    }
};
ll m,n,b,pre[N],dis[N],f[N];
bool done[N];
ll maxx=-1,ans=-1,l,r;
bool check(ll x)
{
    if(f[1]>x) return 0;
    memset(dis,0x7f,sizeof(dis));
    memset(done,0,sizeof(done));
    dis[1]=0;
    priority_queue<node,vector<node>,cmp> q;
    q.push(node(1,dis[1]));
    while(!q.empty())
    {
        //printf("!!!\n");
        node u=q.top();
        q.pop();
        if(done[u.id])continue;
        done[u.id]=true;
        for(ll i=0;i<e[u.id].size();i++)
        {
            edge y=e[u.id][i];
            //if(done[y.to]) continue;
            if(f[y.to]>x) continue;
            if(dis[y.to]>y.w+u.dis)
            {
                dis[y.to]=y.w+u.dis;
                q.push(node(y.to,dis[y.to]));
                if(y.to==n)
                {
                    if(dis[n]>=b) return 0;
                    else return 1;
                }
            }

        }
    }
    return 0;
}
int main()
{
    scanf("%lld%lld%lld",&n,&m,&b);
    for(ll i=1;i<=n;i++) scanf("%lld",&f[i]),maxx=max(maxx,f[i]);

    while(m--)
    {
        ll u,v,w;
        scanf("%lld%lld%lld",&u,&v,&w);
        e[u].push_back(edge(u,v,w));
        e[u].push_back(edge(v,u,w));
    } 
    l=1,r=maxx;
    while(l<=r)
    {
        ll mid=(l+r)>>1;
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    if(ans==-1) printf("AFK");
    else printf("%lld",ans);
    return 0;
}

|