HDU 3586 Information Disturbing 树形DP+二分

该博客介绍了如何使用树形动态规划和二分查找策略,来解决一个军事通信干扰问题。任务是在不超过设备能力限制的情况下,切断敌方通信网络,使得前线士兵无法将信息传递给总指挥。博主分析了问题并提供了相应的算法实现,包括输入输出格式和样例测试。
摘要由CSDN通过智能技术生成

题目描述:

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个点,1点为根结点,每条边有一个权表示将这个边去除需要多少,你的任务是分割边,将所有叶子节点与根结点不连通,但是要保证权之和不能超过m,输出这些被拆的边权的最大值。
二分查找所有权的和,深搜更新dp值。

代码如下:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 1100000;//INF值只需比m稍大即可太大反而会溢出
const int MAXN = 1010;

struct node
{
    int u,v,w,next;
}mp[MAXN<<2];

int dp[MAXN];//dp[i]为i为父节点下删除叶子节点的最小值
int maxx;
int mid;
int head[MAXN],id;
void init()
{
    memset(head,-1,sizeof(head));
    id=0;
}

void addedge(int u,int v,int w)
{
    mp[id].u=u;
    mp[id].v=v;
    mp[id].w=w;
    mp[id].next=head[u];
    head[u]=id++;
}

void dfs(int u,int pre)
{
    bool flag=false;
    for(int i=head[u]; i!=-1; i=mp[i].next)
    {
        int v=mp[i].v;
        if (v==pre) continue;//叶子节点
        flag=true;
        dfs(v,u);
    }
    if (!flag) {dp[u]=INF; return;}
    for(int i=head[u]; i!=-1; i=mp[i].next)
    {
        int w=mp[i].w;
        int v=mp[i].v;
        if(v==pre)continue;
        if(w<=mid)//符合条件
           dp[u]+=min(dp[v],w);
        else//不符合条件
           dp[u]+=min(dp[v],INF);
    }
}

int main()
{
    int m,n;
    while(~scanf("%d%d",&n,&m) && (n || m))
    {
        init();
        maxx=0;
        for(int i=1; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            maxx+=w;//所有权的和
        }
        int l=0,r=maxx;
        bool flag=false;
        while(l<r)
        {
            mid=(l+r)>>1;
            memset(dp,0,sizeof(dp));
            dfs(1,-1);
            if (dp[1]<=m)
            {
                flag=true;//找到符合条件的值
                r=mid;
            }
            else l=mid+1;
        }
        if (!flag) printf("-1\n");
        else  printf("%d\n",r);//最后r值为二分结果
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值