HDU3586 Information Disturbing 树形dp+二分

2023-10-24 07:30

本文主要是介绍HDU3586 Information Disturbing 树形dp+二分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接

Information Disturbing

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 5602    Accepted Submission(s): 1811


 

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

 

最大边的最小值,二分;

dp[u]表示切断u子树里所有叶子节点的最小花费(切的每一条边不超过mid的时候),从儿子节点向父亲节点即从下向上转移;

设v是u的儿子节点,切除u每一个儿子v的所有叶子节点有两种方案,一种是切断uv之间的边,一种是按照v的方案;如果u,v之间边权大于mid,只能按照v的方案;如果边权小于mid,则取min。

听不懂就看代码吧,简单易懂。

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#define LL long long
#define INF 0x3f3f3f3f
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=1e3+10; 
struct node
{int v,w;node (){}node(int v,int w):v(v),w(w){}	
};
vector<node>tr[N];
int n,m,l,r,ans,mid,dp[N];
void dfs(int u,int fa)
{if(tr[u].size()==1&&u!=1)dp[u]=m+1;//叶子节点初始化elsedp[u]=0;//非叶子节点初始化for(int i=0;i<tr[u].size();i++){int v=tr[u][i].v,w=tr[u][i].w;if(v==fa) continue;dfs(v,u);if(w>mid)//边权大于mid不能选用dp[u]+=dp[v];elsedp[u]+=min(w,dp[v]);//选取一个最小值}
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);while(cin>>n>>m,n+m){r=0;int u,v,w;for(int i=1;i<=n;i++)tr[i].clear();for(int i=1;i<n;i++){cin>>u>>v>>w;tr[u].push_back(node(v,w));tr[v].push_back(node(u,w));r=max(r,w);//右边界}l=1,ans=-1;//没有答案输出-1,所以ans初始化-1while(l<=r)//二分{mid=(l+r)>>1;dfs(1,0); if(dp[1]<=m){ans=mid;r=mid-1;}elsel=mid+1;}cout<<ans<<'\n';}
}
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

这篇关于HDU3586 Information Disturbing 树形dp+二分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/273655

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

uva 10154 DP 叠乌龟

题意: 给你几只乌龟,每只乌龟有自身的重量和力量。 每只乌龟的力量可以承受自身体重和在其上的几只乌龟的体重和内。 问最多能叠放几只乌龟。 解析: 先将乌龟按力量从小到大排列。 然后dp的时候从前往后叠,状态转移方程: dp[i][j] = dp[i - 1][j];if (dp[i - 1][j - 1] != inf && dp[i - 1][j - 1] <= t[i]

uva 10118 dP

题意: 给4列篮子,每次从某一列开始无放回拿蜡烛放入篮子里,并且篮子最多只能放5支蜡烛,数字代表蜡烛的颜色。 当拿出当前颜色的蜡烛在篮子里存在时,猪脚可以把蜡烛带回家。 问最多拿多少只蜡烛。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cs

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl

uva 10029 HASH + DP

题意: 给一个字典,里面有好多单词。单词可以由增加、删除、变换,变成另一个单词,问能变换的最长单词长度。 解析: HASH+dp 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc