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. ...
随机推荐
- npm包管理工具
# 从官方包仓库中查找forever包的信息 npm search forever # 查看forever包中package.json的信息 npm view forever # 下载,安装forev ...
- thread block grid
grid里面包含block,block里面包含thread grid里面所有的block都是同样大小的, 每个block最多可以有1024个thread. blockDim表示一个block里面th ...
- Linux常见面试题
一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的文件系统.3. Linux文件系统中每个文件用 索引节点来标 ...
- 七、Solr服务部署和安全
概念: 我们知道,Solr是以webapp的形式运行的,那么我们只需要把Solr.war文件部署到web容器中,便可以运行了,但是因为需要连接数据库做索引并且提供线上的服务调用query接口,那么So ...
- Messager( 消息窗口) 组件
一. 加载方式消息窗口提供了不同的消息框风格,包含 alert(警告框).confirm(确认框).prompt(提示框).progress(进度框)等.所有消息框都是异步的,用户可以在交互消息之后使 ...
- 【VS2015正式版下载】Visual Studio 2015 正式版开放下载 Visual Studio 2015 神key
说明: 微软定于2015年7月20日发布Visual Studio 2015正式版,目前其官方网站已经提供正式版本的下载. 可在https://www.visualstudio.com/en-us/d ...
- winform中获取Properties窗口的值.
我写这个工具,主要是多次在将自己的代码和别人代码做对比时,不想繁琐地用眼看他设置的和自己设置的哪里不一样. using System; using System.Collections.Generic ...
- displaytag 简单使用流程
1. 首先导入包:displaytag-1.2.jar,commons-lang.jar和standard.jar;commons-beanutils.jar;这四个jar包 2. 然后在jsp页面做 ...
- 活动指示器UIActivityIndicatorView
活动指示器UIActivityIndicatorView可以告知用户有一个操作正在进行中 1.创建 UIActivityIndicatorView *activityIndicatorView ...
- POJ 1930 Dead Fraction
POJ 1930 Dead Rraction 此题是一个将无限循环小数转化为分数的题目 对于一个数 x=0.abcdefdef.... 假设其不循环部分的长度为m(如abc的长度为m),循环节的长度为 ...