hdu 3586 Information Disturbing

本文探讨了一种在虚拟战场环境中破坏敌方通讯网络的方法。通过分析敌军的通讯结构,利用特定设备切断关键线路,确保前线士兵无法将情报传递给指挥官。采用树形动态规划算法结合二分查找技术,寻找最小成本方案。

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


Information Disturbing

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


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
 

Author
alpc86
 

Source
 

Recommend
zhouzeyong
 

Statistic |  Submit |  Discuss |  Note
题意:给你一棵树,1节点为总部,只有一条边相连的点为前线战士,边有权值,问存不存在切断总部和前线战士所需的总费用<=m的方案,如果存在输出最小总费用方案中最大的边权值。

思路:一看就是树形dp,单论存不存在这是一道水题,直接dp就好,明显有动态转移方程:dp[根]+=max(dp[子],边权值),但问题要求出最大边权值,这里要巧妙的利用二分求出答案,把边权值在所要范围内的才累加,若最后dp[1]>m,证明范围太小,要扩大范围,反之缩小范围,然后一直重复dp直到求出答案,若范围给到最大还是>m,证明不存在。下面给代码:

#include<iostream>
#include<cmath>
#include<queue>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#define maxn 10005
#define inf 0x3f3f3f3f
using namespace std;
int head[maxn],dp[maxn];
int n,m;
struct node{
    int u,value,next;
}p[maxn<<1];
void dfs(int x,int fa,int limit){
    bool jud=true;
    dp[x]=0;
    for(int i=head[x];~i;i=p[i].next){
        int next=p[i].u;
        if(next==fa)
          continue;
        dfs(next,x,limit);
        jud=false;
        if(p[i].value<=limit){
            dp[x]+=min(dp[next],p[i].value);
        }
        else{
            dp[x]+=dp[next];
            dp[x]=min(dp[x],inf);
        }
    }
    if(jud)
      dp[x]=inf;
}
int main(){
    while(scanf("%d%d",&n,&m)&&(n+m)){
        memset(head,-1,sizeof(head));
        int maxnum=0;
        for(int i=0;i<((n-1)<<1);i++){
            int u,v,value;
            scanf("%d%d%d",&u,&v,&value);
            p[i].u=u;
            p[i].value=value;
            p[i].next=head[v];
            head[v]=i++;
            p[i].u=v;
            p[i].value=value;
            p[i].next=head[u];
            head[u]=i;
            maxnum=max(maxnum,value);
        }
        int l=1,r=maxnum,mid;
        int ans=-1;
        while(l<=r){
            mid=(l+r)>>1;
            dfs(1,0,mid);
            if(dp[1]<=m){
                r=mid-1;
                ans=mid;
            }
            else{
                l=mid+1;
            }
        }
        printf("%d\n",ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值