哪位朋友能告诉我为什么不用二分AC不了,70pts

P1462 通往奥格瑞玛的道路

Herbie_ZHB @ 2024-02-17 09:28:21

#include<bits/stdc++.h>
using namespace std;
#define N 10010
#define M 50010 
int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
int n,m,b,ans=0x7fffffff;
int f[N];
int h[N],cnt;
bool vis[N];
struct Edge{
    int nxt,to,c;
}e[M*2];
void add(int a,int b,int c){
    cnt++;e[cnt].to=b;e[cnt].c=c;e[cnt].nxt=h[a];h[a]=cnt;
}
struct node{
    int maxedge,to;
    long long csum;
}tmp;
queue<node> q;
void spfa(){
    tmp.maxedge=f[1];tmp.csum=0;tmp.to=1;
    q.push(tmp);
    //cout<<111<<endl;
    while(!q.empty()){
        node u=q.front();q.pop();
        if(vis[u.to])continue;
        //cout<<u.to<<endl;
        vis[u.to]=1; 
        for(int i=h[u.to];i;i=e[i].nxt){
            int v=e[i].to,w=e[i].c;
            //if(vis[v])continue;
            //cout<<u.to<<' '<<v<<endl;
            tmp.maxedge=max(u.maxedge,f[v]);
            tmp.csum=u.csum+w;
            tmp.to=v;
            //cout<<tmp.csum<<endl;
            if(tmp.csum>b)continue;
            if(v==n){
                ans=min(ans,tmp.maxedge);
                continue;
            }
            q.push(tmp);
        }
    }
}
int main(){
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    n=read();m=read();b=read();
    //cout<<n<<' '<<m<<' '<<b<<endl;
    for(int i=1;i<=n;i++){
        f[i]=read();
        //cout<<f[i]<<' ';
    }//cout<<endl;
    for(int i=1;i<=m;i++){
        int u,v,w;
        u=read();v=read();w=read();
        add(u,v,w);
        add(v,u,w);
        //cout<<u<<' '<<v<<' '<<w<<endl;
        //cout<<cnt<<endl;
    }
    spfa();
    if(0x7fffffff==ans){
        cout<<"AFK"<<endl;
    }else cout<<ans<<endl;
    return 0;
}

|