当前位置 : 主页 > 手机开发 > harmonyos >

hdu 3586(树形dp+二分)

来源:互联网 收集:自由互联 发布时间:2023-08-28
Information Disturbing Time Limit: 6000/3000 MS (Java/Others)Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2540Accepted Submission(s): 901 Problem Description In the battlefield , an effective way to defeat enemies is to b


Information Disturbing



Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)


Total Submission(s): 2540    Accepted Submission(s): 901





Problem Description


In the battlefield , an effective way to defeat enemies is to break their communication system.
The information department told you that there are n enemy soldiers and their network which have n-1 communication routes can cover all of their soldiers. Information can exchange between any two soldiers by the communication routes. The number 1 soldier is the total commander and other soldiers who have only one neighbour is the frontline soldier.
Your boss zzn ordered you to cut off some routes to make any frontline soldiers in the network cannot reflect the information they collect from the battlefield to the total commander( number 1 soldier).
There is a kind of device who can choose some routes to cut off . But the cost (w) of any route you choose to cut off can’t be more than the device’s upper limit power. And the sum of the cost can’t be more than the device’s life m.
Now please minimize the upper limit power of your device to finish your task.


 



Input


The input consists of several test cases. 
The first line of each test case contains 2 integers: n(n<=1000)m(m<=1000000).
Each of the following N-1 lines is of the form:
ai bi wi
It means there’s one route from ai to bi(undirected) and it takes wi cost to cut off the route with the device.
(1<=ai,bi<=n,1<=wi<=1000)
The input ends with n=m=0.


 



Output


Each case should output one integer, the minimal possible upper limit power of your device to finish your task. 
If there is no way to finish the task, output -1.


 



Sample Input


5 5 1 3 2 1 4 3 3 5 5 4 2 6 0 0


 



Sample Output


3


 


 //二分limit 每次dfs查找最小的花费 dp[i] 以i为根节点切断与所有字树的最小代价 叶子为无穷大


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e3+5;
const int inf=1e6+5;
vector<pair<int,int> >G[N];
ll dp[N];
void dfs(int v,int p,int lim)
{
    ll tol=0,flag=0;
    for(int i=0;i<G[v].size();i++)
    {
        ll u=G[v][i].first;
        ll len=G[v][i].second;
        if(u==p)continue;
        dfs(u,v,lim);
        flag=1;
        if(len<=lim) tol+=min(dp[u],len);
        else tol+=dp[u];
    }
    dp[v]=flag?tol:inf;
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        for(int i=1;i<=n;i++)
            G[i].clear();
        for(int i=1;i<n;i++)
        {
            int u,v,len;
            scanf("%d%d%d",&u,&v,&len);
            G[u].push_back({v,len});
            G[v].push_back({u,len});
        }
        int l=1,r=1000;
        while(l<=r)
        {
            int mid=(l+r)/2;
            for(int i=1;i<=n;i++)
                dp[i]=0;
            dfs(1,0,mid);
            if(dp[1]<=m)
                r=mid-1;
            else
                l=mid+1;
        }
        if(l<1||l>1000)
            puts("-1");
        else
            printf("%d\n",l);
    }

    return 0;
}




网友评论