题目描述

Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4003 Accepted Submission(s): 1391

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个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=1000000

思路

  • 二分+树形dp
  • 二分可能的limit,再判断该limit下的花费是否符合m;
  • dp: 对于一个节点u及其子节点v,要切断u子树下所有叶子节点与u的联系,有两种方式.
  • 设dp[u]表示切断u子树下所有叶子节点与u的联系的最小花费
  1. 不切断u->v,$dp[u]+=dp[v]$;
  2. 切断u->v,$dp[u]+=dis(u,v)$;

代码

#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#define re register int
using namespace std;
const int maxn=1e3+50;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
int head[maxn<<1];
long long dp[maxn];
int tot=1,INF=1e6;
struct data{
int to,nxt;
long long w;
}edge[maxn<<1];
void DFS(int u,int fa,int lim){
int flag=0;
dp[u]=0;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v!=fa){
flag=1;
DFS(v,u,lim);
if(edge[i].w<=lim)
dp[u]+=min(dp[v],edge[i].w);
else
dp[u]+=dp[v];
}
}
if(!flag) dp[u]=INF;
}
inline void add(int u,int v,int w){
edge[tot].to=v;
edge[tot].w=w;
edge[tot].nxt=head[u];
head[u]=tot++;
}
int main(){
int MAXX=-1;
int n,m;
while(~scanf("%d%d",&n,&m)) {
// cout<<n<<m<<endl;
if(n==0&&m==0) break;
tot=1;
memset(head,0,sizeof(head));
for(re i=1;i<n;++i) {
int a,b,w;
a=read(),b=read(),w=read();
add(a,b,w);
add(b,a,w);
MAXX=max(MAXX,w);
}
int l=0,r=MAXX,ans=-1,mid;
while(l<=r) {
//cout<<endl;
mid=(l+r)>>1;
DFS(1,-1,mid);
//cout<<l<<" "<<r<<endl;
//cout<<dp[1]<<endl;
if(dp[1]<=m){
ans=mid;
r=mid-1;
}else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
} /*
5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0
*/

【题解】hdu 3586 Information Disturbing 二分 树形dp的更多相关文章

  1. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  2. hdu 3586 Information Disturbing(树形dp + 二分)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:   hdu-3586 题意 给一棵n个节点的树,节点编号为1-n,根节点为1.每条边有权值,砍掉一条边要花费 ...

  3. HDU 3586 Information Disturbing (树形DP,二分)

    题意: 给定一个敌人的通信系统,是一棵树形,每个节点是一个敌人士兵,根节点是commander,叶子是前线,我们的目的是使得敌人的前线无法将消息传到commander,需要切断一些边,切断每条边需要一 ...

  4. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

  5. HDU 3586 Information Disturbing 树形DP+二分

    Information Disturbing Problem Description   In the battlefield , an effective way to defeat enemies ...

  6. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

  7. HDU 3586.Information Disturbing 树形dp 叶子和根不联通的最小代价

    Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/ ...

  8. hdu3586 Information Disturbing 【树形dp】

    题目链接 hdu3586 题解 二分 + 简单的树形dp 我正有练一下dp的必要了 #include<iostream> #include<cstdio> #include&l ...

  9. HDU-3586 Information Disturbing(树形DP+删边)

    题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的 ...

随机推荐

  1. @shiro.hasPermission 使用

    在页面上加上@shiro.hasPermission 如下用.ftl为例子: 当加上shiro标签后,会与后台代码结合使用: 需要继承AuthorizingRealm  下的 protected Au ...

  2. Electron-Vue3-Vadmin后台系统|vite2+electron桌面端权限管理系统

    基于vite2.x+electron12桌面端后台管理系统Vite2ElectronVAdmin. 继上一次分享vite2整合electron搭建后台框架,这次带来的是最新开发的跨桌面中后台权限管理系 ...

  3. Codeforces Round #660 (Div. 2)

    A. Captain Flint and Crew Recruitment 题意:定义了一种数(接近质数),这种数可以写成p*q并且p和q都是素数,问n是否可以写成四个不同的数的和,并且保证至少三个数 ...

  4. docker 的常见命令行解析

    1.查看本地镜像 sudo docker images 2.查看本地容器 sudo docker ps 3.根据Dockerfile制作镜像命令 sudo docker build -t Myimag ...

  5. 发现数据结构与算法之美的第n次重新学习 ——— 初遇数据结构与算法(了解)

    你的数据结构怎么学的?提起数据结构,计算机与软件,it行业内无人不知,无人不晓.但是,当你真正的去通过数据结构与算法内容去实践内容时,真的能联系起来吗?那肯定的 不管是考研还是做项目,数据结构都是必学 ...

  6. select 语句的基础语法

    授权语法sql 一.用户的创建与使用 在管理员登录后可创建用户 --创建qfplan用户-create user qfplan identified by qfplan; --用户基本权限授权gran ...

  7. [rhel-media] :Yum软件仓库唯一标识符,避免与其他仓库冲突。

    第1步:进入到/etc/yum.repos.d/目录中(因为该目录存放着Yum软件仓库的配置文件). 第2步:使用Vim编辑器创建一个名为rhel7.repo的新配置文件(文件名称可随意,但后缀必须为 ...

  8. 开机自动挂载本地yum源-20200402-V0.1

    开机自动挂载本地yum源-20200402-V0.1 已下载本地iso /home/Kylin-Server-10-mips64-Release-Build04.08-lic-20200313.iso ...

  9. C++知识点案例 笔记-4

    1.纯虚函数 2.抽象类 3.内部类 4.运算符重载 5.类的函数重载 6.友元的函数重载 1.纯虚函数 ==纯虚函数== //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数 ...

  10. ssh创建与添加密钥开启免密登陆 免确认机器指纹参数

     主要是两个步骤 1.控制主机创建密钥对(私钥和公钥) 2.把密钥对的公钥加入对方的认证列表中 [root@vps ~]# ssh-keygen [root@vps ~]# ssh-copy-id u ...