HDU - 3586 Information Disturbing【树形dp+二分】

 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

题意:编号1为总司令点,叶子节点为士兵点,要求总司令不能接收任一士兵的情报。剪断一条线路有花费,总花费不能超过M,求最小的单次花费(只要总花费不超过m即可).
思路:(树形dp + 二分)二分单次花费上限,再用树形dp查询最小总花费与m比较。树形dp查询时,采用之前总结的向下查询向上更新,递推方程:
if(e.cost <= mid) dp[x] += (!dp[e.to] ? e.cost : (min(dp[e.to], e.cost))) ;
else dp[x] += (!dp[e.to] ? INF : dp[e.to]) ;
这道题一开始理解错题意了,看到n那么小,zz了一波出题人,后来重新推方程,可是怎么都错,竟然是INF赋值太大(坑了一天,一万个mmmmp).

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#define INF 1000000 + 10 //无限个mmmmmmp**
using namespace std;

const int MAXN = 1 * 1e3 + 5;
int dp[MAXN];
int n, m, ans;

struct node {
    int to;
    int cost;
};

vector<node> v[MAXN];

void dfs(int x, int fa, int mid) {
    for(int i = 0; i < v[x].size(); i++) {
        node e = v[x][i];
        if(e.to == fa) continue;
        dfs(e.to, x, mid);
        if(e.cost <= mid) {
            dp[x] += (!dp[e.to] ? e.cost : (min(dp[e.to], e.cost)));
        }
        else {
            dp[x] += (!dp[e.to] ? INF : dp[e.to]);
        }
    }
}

int main() {
    while(scanf("%d %d", &n, &m), (n + m)) {
        for(int i = 0; i < MAXN; i++) {
            v[i].clear(); 
        }
        for(int i = 1; i < n; i++) {
            int p1, p2, cos;
            scanf("%d %d %d", &p1, &p2, &cos);
            node e;
            e.to = p2;
            e.cost = cos;
            v[p1].push_back(e);
            e.to = p1;
            e.cost = cos;
            v[p2].push_back(e);
        }
        int l = 0, r = INF, mid;
        while(r >= l) {
            mid = (l + r) >> 1;
            memset(dp, 0, sizeof(dp));
            dfs(1, -1, mid);
            if(dp[1] > m) {
                l = mid + 1;
            }
            else r = mid - 1;
        }
        memset(dp, 0, sizeof(dp));
        dfs(1, -1, l);
//      for(int i = 1; i <= n; i++) {
//          printf("%d -> %d\n", i, dp[i]);
//      }
//      printf("%d\n", dp[1]);
        if(dp[1] <= m) printf("%d\n", l);
        else printf("-1\n");
    }
    return 0;
}


/*
6 5
1 2 8
1 3 2
2 4 3
2 5 2
4 6 1

5 5
1 3 2
1 4 3
3 5 5
4 2 6
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值