求助:91分wa#4(看过别人的贴子改过但还是wa)

P1462 通往奥格瑞玛的道路

dadaaa @ 2022-02-07 18:24:24

#include<iostream>
#include<algorithm>
#include<climits>
#include<cstdio>
#include<queue>
using namespace std;
const int N=10001,M=50001;
int head[N],num,nxt[2*M],to[2*M],val[2*M];
void add(int f,int t,int v){
    to[++num]=t;val[num]=v;
    nxt[num]=head[f];head[f]=num;
}
int dis[N],vis[N];
int n,m,blood,fy[N],copy_fy[N];
bool check(int threshod){
    for(int i=1;i<=n;i++) dis[i]=INT_MAX,vis[i]=false;
    if(fy[1]>threshod) return false;
    priority_queue<pair<int,int> > q;
    q.push(make_pair(0,1));
    dis[1]=0;
    while(!q.empty()){
        int x=q.top().second;q.pop();
        if(vis[x]) continue;
        vis[x]=true;
        for(int v,i=head[x];i;i=nxt[i]){
            v=to[i];
            if(vis[v]||fy[v]>threshod||dis[v]<=dis[x]+val[i]) continue;
            dis[v]=dis[x]+val[i];
            q.push(make_pair(-dis[v],v));
        }
    }
    if(dis[n]>=blood) return false;
    else return true;
}
bool cmp(int a,int b){
    return a<b;
}
int ans=0;
void fz(int l,int r){
    if(l>r){
        if(ans) cout<<ans;
        else cout<<"AFK";
        return; 
    }
    int mid=(l+r)>>1;
    if(check(copy_fy[mid])){
        r=mid-1;
        ans=copy_fy[mid];
    } else {
        l=mid+1;
    }
    fz(l,r);
}
int main() {
    cin>>n>>m>>blood;
    for(int i=1;i<=n;i++){
        cin>>fy[i];
        copy_fy[i]=fy[i];
    }
    for(int i=1;i<=m;i++){
        int a,b,c;cin>>a>>b>>c;
        if(a==b) continue;//已去自环
        add(a,b,c);
        add(b,a,c);
    }
    sort(copy_fy+1,copy_fy+n+1,cmp);
    if(!check(copy_fy[n])) {
        cout<<"AFK";
        return 0;
    }
    fz(1,n);
    return 0;
}

by dadaaa @ 2022-02-07 18:25:27

算法:dij……


|