题目描述

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)$;

代码

  1. #include<cstdio>
  2. #include<string>
  3. #include<vector>
  4. #include<cstring>
  5. #include<iostream>
  6. #define re register int
  7. using namespace std;
  8. const int maxn=1e3+50;
  9. inline int read(){
  10. int x=0,w=1;
  11. char ch=getchar();
  12. while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
  13. if(ch=='-') w=-1,ch=getchar();
  14. while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
  15. return x*w;
  16. }
  17. int head[maxn<<1];
  18. long long dp[maxn];
  19. int tot=1,INF=1e6;
  20. struct data{
  21. int to,nxt;
  22. long long w;
  23. }edge[maxn<<1];
  24. void DFS(int u,int fa,int lim){
  25. int flag=0;
  26. dp[u]=0;
  27. for(int i=head[u];i;i=edge[i].nxt){
  28. int v=edge[i].to;
  29. if(v!=fa){
  30. flag=1;
  31. DFS(v,u,lim);
  32. if(edge[i].w<=lim)
  33. dp[u]+=min(dp[v],edge[i].w);
  34. else
  35. dp[u]+=dp[v];
  36. }
  37. }
  38. if(!flag) dp[u]=INF;
  39. }
  40. inline void add(int u,int v,int w){
  41. edge[tot].to=v;
  42. edge[tot].w=w;
  43. edge[tot].nxt=head[u];
  44. head[u]=tot++;
  45. }
  46. int main(){
  47. int MAXX=-1;
  48. int n,m;
  49. while(~scanf("%d%d",&n,&m)) {
  50. // cout<<n<<m<<endl;
  51. if(n==0&&m==0) break;
  52. tot=1;
  53. memset(head,0,sizeof(head));
  54. for(re i=1;i<n;++i) {
  55. int a,b,w;
  56. a=read(),b=read(),w=read();
  57. add(a,b,w);
  58. add(b,a,w);
  59. MAXX=max(MAXX,w);
  60. }
  61. int l=0,r=MAXX,ans=-1,mid;
  62. while(l<=r) {
  63. //cout<<endl;
  64. mid=(l+r)>>1;
  65. DFS(1,-1,mid);
  66. //cout<<l<<" "<<r<<endl;
  67. //cout<<dp[1]<<endl;
  68. if(dp[1]<=m){
  69. ans=mid;
  70. r=mid-1;
  71. }else l=mid+1;
  72. }
  73. printf("%d\n",ans);
  74. }
  75. return 0;
  76. }
  77.  
  78. /*
  79. 5 5
  80. 1 3 2
  81. 1 4 3
  82. 3 5 5
  83. 4 2 6
  84. 0 0
  85. */

【题解】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. 【.Net Core】分析.net core在linux下内存占用过高问题

    现象 随着程序运行,内存占用率越来越高,直到触发linux的OOM,程序被杀死. 分析工具 运行环境:.net core 3.1(微软的分析工具要求最低3.0,无法分析2.1的core程序,需要先改为 ...

  2. springboot开发浅谈 2021/05/11

    学习了这么久,本人希望有时间能分享一下,这才写下这篇浅谈,谈谈软件,散散心情. 这是本人的博客园账号,欢迎关注,一起学习. 一开始学习springboot,看了好多网站,搜了好多课程.零零落落学了一些 ...

  3. Unity动态构建mesh绘制多边形算法流程分析和实践

    前言 先说一下,写这篇博文的动机,原文的博主代码写的十分潇洒,以至于代码说明和注释都没有,最近恰逢看到,所以以此博文来分析其中的算法和流程 参考博文:https://blog.csdn.net/lin ...

  4. 认识 Spring Cloud Alibaba

    个人理解 Spring Cloud Alibaba 就是 Spring Cloud 的微服务规范的一种实现,外加一些阿里云的商业组件 Spring Cloud 是什么 Spring Cloud 为开发 ...

  5. [bug] Python Anoconda3 安装完成后开始菜单不显示

    版本问题,需更新 win+R打开cmd,敲入命令: conda update menuinst conda install -f console_shortcut ipython ipython-no ...

  6. 如何在CentOS 7上安装Htop

    在本教程中,我们将向您介绍如何在CentOS 7服务器上安装和配置Htop.对于那些不知道的人,Htop 是为Linux编写的一个交互式实时系统监视进程查看器.它被设计为替代Unix程序的顶部.它显示 ...

  7. Linux性能监控与分析之--- CPU

    Linux性能监控与分析之--- CPU 望月成三人关注 2016.07.25 18:16:12字数 1,576阅读 2,837 CPU性能指标 用户进程使用CPU的比率 系统进程使用CPU的比率 W ...

  8. 如何访问pod --- service(7)

    一.通过service访问pod 我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 controller ...

  9. Linux 发行版本介绍

    引言 Linux 有非常多的版本,比如世面上常见的有 Ubuntu.RedHat.Fedora.Centos 等,这么多的版本我们究竟该选哪一个呢?对于 Linux 初学者有必要对这些 Linux 发 ...

  10. VS Code 安装后的一些配置项

    说明: 个人一直使用Notepad++作为日常文本编辑器,由于之前出现的某个原因,故决定改用VS Code. •设置中文字体 • 输入快捷键 Ctrl+Shift+P • 输入 Configure D ...