HDU3586Information Disturbing 二分+树形dp

题意:

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

​ 这道题就是说给出n,m分别代表节点数和权值,再给出n-1条边ai,bi,wi,代表ai与bi之间有一条无向路,道路权值为wi。问在截断的道路总权值不超过m的前提下使得所有的叶节点都不能与跟节点相连,截断的道路中最大的权值尽可能的小,问最小值是多少?

思路:

​ 一道较难的树形dp题,我们一般看到在什么什么的前提下,使得某个值最小或者最大。就应该想到用二分来求解。我们通过二分最小值,然后满足所截道路的权值都小于等于二分值进行dfs。最后找到最小满足条件的即是答案。

代码:

#include <stdio.h>
#include <vector>
#include <string.h>
#define N 1005
using namespace std;

struct node {
    int inx, val;
};

vector<node>point[N];
int n, m;
int dp [N];

void init() {
    int a, b, c;
    node t;
    for (int i = 1; i <= n; i++) {
        point[i].clear();
    }
    for (int i = 0; i < n - 1; i++) {
        scanf("%d%d%d", &a, &b, &c);
        t.val = c;
        t.inx = b;
        point[a].push_back(t);
        t.inx = a;
        point[b].push_back(t);
    }
}

bool dfs(int inx, int father, int w) {
    int sum = 0;
    for (int i = 0; i < point[inx].size(); i++) {
        node son = point[inx][i];
        if (son.inx == father) continue;
        dfs(son.inx, inx, w);
        if (dp[son.inx] == 0) {     //当儿子节点没有成立的截断
            if (son.val <= w) {
                sum += son.val;
            } else {
                return false;
            }
        } else {                    //当儿子节点有成立的截断
            if (son.val <= w) {
                sum += min(dp[son.inx], son.val);
            } else {
                sum += dp[son.inx];
            }
        }
    }
    dp[inx] = sum;
    return true;
}

int erfen() {
    int star = 1, endd = m;
    int ans = -1;
    while (star <= endd) {
        int mid = (star + endd) >> 1;
        memset(dp, 0, sizeof(dp));
        bool flag = dfs(1, -1, mid);
        if (flag == true && dp[1] <= m) {
            ans = mid;
            endd = mid - 1;
        } else {
            star = mid + 1;
        }
    }
    return ans;
}

int main () {
    while ((scanf("%d%d", &n, &m) == 2) && (n || m)) {
        init();
        int ans = erfen();
        printf("%d\n", ans);
    }
    return 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值