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. ...
随机推荐
- Oracle_Q&A_01
Step01: SHOW USER DESCRIPTION Step02:QUESTION & ANSWER --查询员工姓名和职位字数相等的员工 SELECT * from emp wher ...
- pods 这两篇就够了
http://www.cnblogs.com/gongyuhonglou/p/5801681.html http://blog.csdn.net/iunion/article/details/1701 ...
- Highcharts 异步加载数据曲线图表
导入 data.js 文件 异步加载数据需要引入以下js 文件: <script src="http://code.highcharts.com/modules/data.js&quo ...
- Js中执行变量中的命令语句,也就是所谓的宏替换(很实用的例子)
Js中执行变量中的命令语句,也就是所谓的宏替换(很实用的例子) 由其做动态编程时非常有用,必须符合js中的语法,用eval能够执行. var aaa="alert('这是变量中的语句')&q ...
- 常用渗透性测试工具(Tools for penetration testing)
常用渗透性测试工具 原文:http://hi.baidu.com/limpid/item/14a2df166adfa8cb38cb3068 对一个应用项目进行渗透性测试一般要经过三个步骤. 第一步, ...
- C# WPF 建立渐隐窗口
需求: 一些无关紧要的提示信息,不显示出来怕用户一头雾水,但如果用对话框显示出来,用户又要动手把对话框关闭.不说别人,就是程序员自己测试时都觉得麻烦! 解决方案: 有两种选择 1. 选择是用 labe ...
- textarea固定大小,不可拖动
写前端,经常很多小东西容易忽略忘记,今天写页面碰到设定一个输入框大小,死活记不起怎么固定,故找了一下度娘,其实添加一个css属性就好了: resize: none; 随笔记一下!
- 3.2:pandas数据的导入与导出【CSV,JSON】
一:CSV数据 一]:导入数据 1)从CSV文件读入数据:pd.read_csv("文件名"),默认以逗号为分隔符 D:\data\ex1.csv文件内容: ...
- No suitable authentication method found to complete authentication (publickey,keyboard-interactive).
string command = Command.Text; StringBuilder result = new StringBuilder(); try { var connectionInfo ...
- MVC部署-发布本地数据库(Localdb)时连接异常
解决方法: 找到对应网站的应用程序池, 在 高级设置 里找到 [标识] 选择为 LocalSystem 就可以了,注意文件的路径和连接字符串.