HDU 3586 : Information Disturbing
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.
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.
If there is no way to finish the task, output -1.
1
4
题目大意:
大意就是给你一棵树(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的更多相关文章
- HDU - 3586 Information Disturbing 树形dp二分答案
HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...
- HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价
Information Disturbing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/ ...
- HDU 3586 Information Disturbing 树形DP+二分
Information Disturbing Problem Description In the battlefield , an effective way to defeat enemies ...
- 【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述 Information DisturbingTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java ...
- HDU 3586 Information Disturbing (二分+树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
- hdu 3586 Information Disturbing(树形dp + 二分)
本文出自 http://blog.csdn.net/shuangde800 题目链接: hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...
- HDU 3586 Information Disturbing (树形DP,二分)
题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...
- BNUOJ 7697 Information Disturbing
Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...
随机推荐
- python游戏编程——跟13岁儿童学编程
python爬虫基本告一段落,琢磨搞点其他的,正好在网上看到一个帖子,一个外国13岁小朋友用python写的下棋程序,内容详细,也有意思,拿来练手. 13岁啊.. 我这年纪还在敲 dir啥的吧 想到原 ...
- I/O多路转接 --- UNIX环境高级编程
I/O多路转接技术:先构造一张有关描述符的列表,然后调用一个函数,知道这些描述符中的一个已准备好进行I/O时,给函数才返回.在返回时,它告诉进程哪些描述符已准备好可以进行I/O. poll.selec ...
- 华为OJ:2041 放苹果
这道题难点不在于代码怎么写,而是思路怎么想. 感觉一般这样的题要么你理好一个思路要么你最后总结出一个公式,要么你自己模拟它的运作方式,用迭代,或者递归的方式来做. 有点像我们曾经学的排列组合. 对于m ...
- codeforces Arrival of the General 题解
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command o ...
- springmvc的讲解
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- mysql 在windows下的安装,开发基础与要点
1:安装(windows下) 官网下载.msi文件 运行安装时只需要安装server就行了 在环境变量中配置到bin目录:e.g:C:\programFile\...mysql\bin 完成后进入wi ...
- Java NIO框架Netty教程(一) – Hello Netty
先啰嗦两句,如果你还不知道Netty是做什么的能做什么.那可以先简单的搜索了解一下.我只能说Netty是一个NIO的框架,可以用于开发分布式的Java程序.具体能做什么,各位可以尽量发挥想象.技术,是 ...
- (转)asp.net注册实现下一步
在asp.net中有两种容器控件,其中包括panel和placeholder控件. 使用panel控件可以对控件进行分组.一帮助组织web窗体也的内容,将控件组织在面板中,可提供有关在运行时控件应如何 ...
- 浅谈MDX处理空值NULL及格式化结果
MDX查询结果中往往会含有"NULL"值,这是某维度下对应的的量值不存在导致的,为了让报表呈现更好的效果,在有些情况下,需要将"NULL"的切片值置换成0,这些 ...
- C#小写人民币转大写
public string GetRMB(decimal moneyAmount) { string s = moneyAmount.ToString("#L#E#D#C#K#E#D#C#J ...