牛客CSP-S提高组赛前集训营3

1.货物收集

 

#include<bits/stdc++.h>
using namespace std;

const int N=1e6+9;
int a[N],w[N],head[N];
struct edge{
    int to,nxt,w;
}e[N<<1];
int n,W,tot;

void add(int u,int v,int p){
    e[++tot].to=v;
    e[tot].nxt=head[u];
    e[tot].w=p;
    head[u]=tot;
}

long long s;
void dfs(int u,int fa,int x){
    for(int i=head[u];i;i=e[i].nxt){
        int v=e[i].to,p=e[i].w;
        if(v==fa) continue;
        if(x>=p){
            s+=a[v];
            dfs(v,u,x);
        }
    }
}

bool check(int mid){
    s=0;
    dfs(1,0,w[mid]);
    return s>=W;//
}

int main(){
    scanf("%d%d",&n,&W);
    for(int i=2;i<=n;i++) 
        scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        int u,v,p;
        scanf("%d%d%d",&u,&v,&p);
        add(u,v,p);
        add(v,u,p);
        w[i]=p;
    }
    sort(w+1,w+n);
    int l=1,r=n-1,mid;
    while(l<r){
        mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    printf("%d\n",w[r]);
}

2.货物分组

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/aprincess/p/11785483.html
今日推荐