求助

P1462 通往奥格瑞玛的道路

Kio_ @ 2020-11-02 21:50:08

思路:二分答案,bfs判断可达性

不知道为啥写挂了......(全WA)

求大佬答疑qwq

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int n,m,b;
int w[10020],px[10020];//w为题目中各点的点权,px为排序后的点权,方便后面二分 
struct pt{
    int id,v;
};
vector<pt> v[10020];
bool check(int vmax){//bfs判断可达性 
    if(w[1]>vmax||w[n]>vmax)return 0;
    queue<pt> q;
    bool pd[10020];
    for(int i=1;i<=n;i++) pd[i] = 0;
    q.push((pt){1,w[1]});
    while(!q.empty()){
        int xid = q.front().id, xv = q.front().v;
        q.pop();
        if(xid == n)return 1;
        if(pd[xid])continue;
        pd[xid] = 1;
        for(int i=0;i<v[xid].size();i++){
            int yid = v[xid][i].id, yv = v[xid][i].v;
            if(!pd[yid] && xv + yv <= b && vmax<=w[yid]) q.push((pt){yid,xv+yv});
        }
    }
    return 0;
}
int read()
{
    int num=0;
    char c=getchar();
    while(c<'0'||c>'9')c=getchar();
    while('0'<=c&&c<='9')num=num*10+c-'0',c=getchar();
    return num;
}
int main(){
    n=read(),m=read(),b=read();
    for(int i=1;i<=n;i++) w[i] = read(), px[i] = w[i], sum+=w[i];
    sort(px+1,px+n+1);
    for(int i=1;i<=m;i++){
        int ui=read(),vi=read(),wi=read();
        v[ui].push_back((pt){vi,wi});
        v[vi].push_back((pt){ui,wi});
    }
    int l=1,r=n;
    while(l<r){
        int mid = (l+r)>>1;
        if(check(px[mid])) r = mid;
        else l = mid + 1;
    }
    printf("%d",px[l]);
}

|