代码求调

P1462 通往奥格瑞玛的道路

wflhx2011 @ 2023-09-24 17:05:12

#include<bits/stdc++.h>
using namespace std;
struct edge
{
    int f,t,l,nex;
}a[1000005];
int head[1000005],tot,f[1000005],l,r,o[1000005],n,m,b,flag[1000005];
void add(int x,int y,int z)
{
    a[++tot].f=x;
    a[tot].t=y;
    a[tot].l=z;
    a[tot].nex=head[x];
    head[x]=tot;
}
struct nod
{
    int w,l;
    bool operator<(const nod &x)const
    {
        return x.w<w;
    }
};
priority_queue<nod> q;
int read()
{
    char c=getchar();
    int x=0,f=1;
    while(c<'0'||c>'9')
    {
        if(c=='-')
            f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool check(int e)
{
//  cout<<e<<endl;
    memset(o,0x3f3f,sizeof(o));
    memset(flag,0,sizeof(flag));
    o[1]=0;
    q.push((nod){o[1],1});
    while(!q.empty())
    {
        int u=q.top().l;
        int d=q.top().w;
        q.pop();
        if(flag[u]) 
            continue;
        flag[u]=1;
        for(int i=head[u];i;i=a[i].nex)
        {
            int v=a[i].t;
            if(f[v]>e)
                continue;
            if(o[v]>o[u]+a[i].l)
            {
                o[v]=o[u]+a[i].l;
                q.push((nod){o[v],v});
            }
        }
    }
    return o[n]<=b;
}
int fen(int l,int r)
{
    int ans;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {   
            r=mid-1;
            ans=mid;
        }
        else
            l=mid+1;
    }
    return ans;
}
int main()
{
    n=read(),m=read(),b=read();
    for(int i=1;i<=n;i++)
    {
        f[i]=read();
        l=min(l,f[i]);
        r=max(r,f[i]);
    }
    int flag=r;
    for(int i=1;i<=n;i++)
    {
        int u=read(),v=read(),w=read();
        add(u,v,w);
        add(v,u,w);
    }
//  cout<<l<<" "<<r<<endl;
    if(check(r)==0)
        cout<<"AFK";
    else
        cout<<fen(l,r);
    return 0;
}

|