hdu 3586 Information Disturbing(二分+树dp)

本文探讨了如何通过切断关键通讯线路来使敌方前线士兵与指挥官失去联系的问题。提出了一种利用二分查找结合树形DP的方法来确定所需设备的最大功率阈值,以确保任务完成的同时成本不超过限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Information Disturbing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1351    Accepted Submission(s): 496


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
题意:给出一棵树,根结点为1,每条边都有一个代价,删除一条边就要付出该边的代价。现在要删去一些边,使得每个叶子结点与根结点都不连通,问删去的边中最大的边最小可以是多少,并且要保证付出的总代价少于m。
思路:二分答案,然后用树dp判断是否满足题目要求。设dp[u]为以u为根的子树切去其下方结点的最小代价。
显然,对于叶子结点,dp[u] = INF;对于非叶子结点,dp[u] = min(dp[v], w(u, v))。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
using namespace std;

const int maxn = 10005;
const int maxm = 1000005;
const int INF = 1 << 20;
struct Edge{
    int v, w, next;
}et[maxn * 2];
int n, m, num, limit;
int eh[maxn], dp[maxn];
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int w){
    Edge e = {v, w, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void dfs(int u, int pre){
    dp[u] = 0;
    for(int i = eh[u]; i != -1; i = et[i].next)
    {
        int v = et[i].v, w = et[i].w;
        if(v == pre) continue;
        if(w > limit) w = INF;
        dfs(v, u);
        dp[u] += min(dp[v], w);
    }
    if(!dp[u]) dp[u] = INF;
}
void solve(){
    int low = 1, high = 1000;
    int ans = -1;
    while(low <= high)
    {
        int mid = (low + high) >> 1;
        limit = mid;
        dfs(1, -1);
        if(dp[1] <= m)
        {
            ans = mid;
            high = mid - 1;
        }
        else low = mid + 1;
    }
    printf("%d\n", ans);
}
int main()
{
    int a, b, c;
    while(scanf("%d%d", &n, &m), n || m)
    {
        init();
        for(int i = 0; i < n - 1; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
            add(b, a, c);
        }
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值