HDU - 3586 Information Disturbing (树形dp + 二分)

11 篇文章 0 订阅
8 篇文章 0 订阅

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

题意:给出N个节点,N-1条边的树,给出每条边权值w,现在要求切断其中一些边,使得任意一个叶子没有走到祖先(1)的路 给出m,要求切断的边的总权值小于等于m,求切断方案中 最大的单个切断边权的值最小

最大值最小 首先想到的是二分 但是怎么二分 首先要想到他的什麼是单调的 当上限越大 他可以切的边权越小 当上限越小他切得单个边权越大  然后可以根据他的这个性质来枚举他的边权的最大值 然后当他的dp[1] > m时 代表他的单个边权小了 所以l = mid + 1; else 满足条件 r = mid + 1;并且记录一下答案 mid

线面看一下我的代码:

#include<bits/stdc++.h>
#define pii pair<int, int>
const int maxn = 1e3 + 5;
const int maxm = 2e6 + 5;
int n, m;
long long dp[maxn];
using namespace std;
vector<pii> e[maxm];

void dfs(int x, int pre, int limit){
    dp[x] = 0;
    int f = 1;
    for(auto i : e[x]){
        int v = i.first;
        long long w = i.second;
        if(v == pre)
            continue;
        f = 0;
        dfs(v, x, limit);
        if(w > limit)
            dp[x] += dp[v];
        else
            dp[x] += min(dp[v], w);
        //if(dp[x] >= 10000000) dp[x] = 10000000;//叶子节点上面w不满足limit 不想开long long可以直接这么写
    }
    if(f) dp[x] = 10000000;//叶子节点最大化值,不影响前面的min
}

int main(){
    ios::sync_with_stdio(0);
    while(cin >> n >> m && n + m){
        for(int u = 1; u <= n; u++)
            e[u].clear();
        for(int K = 1; K < n; K++){
            int a, b, c;
            cin >> a >> b >> c;
            e[a].push_back({b, c});
            e[b].push_back({a, c});
        }
        int l = 1, r = 1000;
        int ans = -1;
        while(l <= r){
            int mid = (l + r) >> 1;
            dfs(1, 0, mid);
            long long tp = dp[1];
            if(tp <= m)
                r = mid - 1, ans = mid;
            else
                l = mid + 1;
        }
        cout << ans << endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值