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<=1000),树的根恒定为1,然后你需要删掉一些边,使得任何叶子都没有路径到达结点1(根).

题目的要求是在删掉的边的总花费不超过m的情况下,最大的花费最小。

解题报告:

首先本题卡了stl的vector,所以需要手写前向星链表.

因为本题中的 m 太大,而单条边的花费最多只有1000,所以我们不妨有 dp( i , j )表示将i为根的子树没有任何叶子可以到达点i,且其中的最大花费是m的最小总边权花费。

那么我们考虑转移,考虑到现在已经处理完了点i的前 k - 1 个子树,现在正在处理点i的第 k 个子树,我们将前面的状态进行转移。

不妨有这条边的花费为 w ,

我们考虑 j 从 0 -> w 的转移,他们的转移只有两种选择,第一种还是维护最大花费为 j , 此时肯定不能切掉这条w的边,只能在子树中切掉他,还有一种就是切掉w这条边,状态转移到dp(u,w_

之后我们考虑 j 从 w + 1 -> 1000 的转移,他们的转移就是取min好了,第一种是在子树中切掉,还有一种是切掉这条w的边,这样我们转移就完成了,初始化dp都是0(因为还没有考虑子树)

同时叶子的话dp初始化成inf,注意转移的时候inf+inf.....可能会爆int,需要谨慎的进行判断.

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e3 + ;
struct Edge
{
int v , w , nxt;
};
int n , m ,dp[maxn][maxn],temp[maxn] , head[maxn],tot,maxv,sz[maxn];
Edge e[maxn*]; void add(int u ,int v ,int w)
{
e[tot].v = v , e[tot].nxt = head[u] , e[tot].w = w;sz[u]++;
head[u] = tot ++ ;
} void initiation()
{
memset(head,-,sizeof(head));tot=;maxv=;memset(sz,,sizeof(sz));
for(int i = ; i < n ; ++ i){int u , v , w;scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);maxv=max(maxv,w);}memset(dp,0x00,sizeof(dp));
} inline void updata(int & x,int v) {x = min(x,v);} void dfs(int u ,int fa)
{
if(sz[u] == && u != ) memset(dp[u],0x3f,sizeof(dp[u]));
for(int i = head[u] ; ~i ; i = e[i].nxt)
{
int v = e[i].v , w = e[i].w;
if(v == fa) continue;dfs(v,u);int newe=dp[v][w]+dp[u][w];
for(int j = ; j <= w ; ++ j)
{
updata(newe,dp[u][j]+w);
if(dp[u][j] >= || dp[v][j] >= ) dp[u][j] = ;
else dp[u][j] += dp[v][j];
}dp[u][w]=newe;
for(int j = w + ; j <= maxv ; ++ j) dp[u][j] += min(dp[v][j] , w);
}
} int solve()
{
dfs(,);int ans = <<;
for(int i = ; i <= maxv ; ++ i) if(dp[][i] <= m) {ans = i;break;}
if(ans == (<<)) ans = -;
return ans;
} int main(int argc , char * argv[])
{
while(scanf("%d%d",&n,&m) && n)
{
initiation();
printf("%d\n",solve());
}
return ;
}

HDU 3586 : Information Disturbing的更多相关文章

  1. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

  2. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  3. HDU 3586 Information Disturbing 树形DP+二分

    Information Disturbing Problem Description   In the battlefield , an effective way to defeat enemies ...

  4. 【题解】hdu 3586 Information Disturbing 二分 树形dp

    题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...

  5. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  6. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

  7. hdu 3586 Information Disturbing(树形dp + 二分)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

  8. HDU 3586 Information Disturbing (树形DP,二分)

    题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...

  9. BNUOJ 7697 Information Disturbing

    Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...

随机推荐

  1. 关于XMLEncoder和XMLDecoder

    我们用XMLEncoder和XMLDecoder来序列化和反序列化一个类. 我觉得需要注意的是,我们在new一个对象的时候,XMLEncoder本身默认的是类中无参的构造函数,我今儿在实现的时候,老是 ...

  2. prototype对象的真正作用

    参考阮一峰的文章:http://javascript.ruanyifeng.com/oop/encapsulation.html prototype对象的真正作用 在JavaScript语言中,每一个 ...

  3. 性能优化之NSDateFormatter

    为什么要优化NSDateFormatter? 优化方式有哪些? 为什么要优化NSDateFormatter? 首先,过度的创建NSDateFormatter用于NSDate与NSString之间转换, ...

  4. js中的call()与apply()

    js中的call()函数和apply()函数: 1.主要作用:是用于指定作用域和传参 (1)用于指定作用域 window.color = "red"; var o = { colo ...

  5. 在Centos 5.6下安装 redis

    先引用redis官方(http://redis.io/) 的介绍: Redis is an open source, advanced key-value store.<br>It is ...

  6. Dubbo亮点总结

    Dubbo是阿里巴巴的一个开源RPC项目,可在http://dubbo.io进行訪问 类似的产品有Hessian.spring httpinvoke 等. Dubbo的亮点总结例如以下: 1.服务注冊 ...

  7. 手机软件记事本(SuperNotepad)的使用教程

    软件简介: 手机应用记事本(SuperNotepad)类似电脑应用notepad, 可用于文本阅读和编辑新建电子书(本应用限文本txt文件),是阅读小说和便签记录的好帮手. 电子书阅读器及便签的手机应 ...

  8. jquery向下取整

    var currentMoney =Math.round((_memberCurrentPoints/_pointVsMoney)*Math.pow(10,2))/Math.pow(10,2) * 2 ...

  9. jquery之onchange事件2

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. 分享一次在Windows Server2012 R2中安装SQL Server2008

    入手一台Windows Server2012云服务器,搭建一下服务环境,选用SQL Server2008 直奔主题,下好安装镜像后,直接双击 选择运行程序而不获取帮助 如图: 进入安装中心后选择 安装 ...