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. 修改cmd的字体

    通常打开的cmd默认的字体比较小,字体只有宋体和新宋体两种,如果要修改,需要通过修改注册表才行. 打开regedit后,找到如下路径HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  2. Chrome真机调试步骤

    确保手机端打开USB调试选项 手机安装chrome 手机访问网页(或者打开APP,或者使用夜深模拟器打开APP或者网页) PC chrome打开chrome://inspect/#devices 点击 ...

  3. java-下载excel

    在java程序里面处理excel,我觉得比较方便的方式是先做出一个excel的模板(比如定义表头信息.表格名称等),然后根据这个模板往里面填充数据 我这里演示的是使用poi处理2007以上版本的exc ...

  4. android避免decodeResource图片时占用太大的内存

    增加largeHeap="true"属性. android:largeHeap Whether your application's processes should be cre ...

  5. 怎么给iOS项目打包

    1  首先要选中项目中的真机測试,不要模拟器 ,然后从上边的菜单条中找product watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29tZXJhaW43 ...

  6. Swift补基础之Selector、条件编译、编译标记、NSObject

    在swift中使用条件编译比较直接 #if <condition> #elseif <condition> #else #endif 例如 :在debug模式和release模 ...

  7. C# WPF 建立渐隐窗口

    需求: 一些无关紧要的提示信息,不显示出来怕用户一头雾水,但如果用对话框显示出来,用户又要动手把对话框关闭.不说别人,就是程序员自己测试时都觉得麻烦! 解决方案: 有两种选择 1. 选择是用 labe ...

  8. HID class request.

    1.get report. 2.set report report request. Get report范例: 下面这张图是Host跟Device来要设备描述符. USB主机向设备控制器请求数据时, ...

  9. Mac下如何不借助第三方工具实现NTFS分区的可写挂载

    问题背景 我想很多使用Mac的同学都会遇到读写NTFS磁盘的问题,因为默认情况下Mac OSX对NTFS磁盘的挂载方式是只读(read-only)的,因此把一个NTFS格式的磁盘插入到Mac上,是只能 ...

  10. 前端 HTML基础

    前端三大利器概述 学习前端,不得不学习前端中的三大利器:html + css + javascript.那么这三个组件分别起到什么作用呢?以人体为例,单单具有html属性的人,只是一个裸体的人偶(理解 ...