Information Disturbing

Time Limit: 3000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 3586
64-bit integer IO format: %I64d      Java class name: Main

 
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

Source

 
 
解题:要求什么寿命之和不能达到最大的寿命值,还要使得那个什么最小。。。最小。。。二分+树形dp。dp[u]表示u节点子树的最小代价。
 
dp[u] += min(dp[g[u][i].to],g[u][i].w);  表示要么不剪g[u][i]这条边,由其子树中的剪,要么剪g[u][i]这条边,子树不减,故取min就是。
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define INF 1000010
using namespace std;
const int maxn = ;
struct arc{
int to,w;
arc(int x = ,int y = ):to(x),w(y){}
};
vector<arc>g[maxn];
int n,m,dp[maxn];
void dfs(int u,int fa,int limit){
for(int i = ; i < g[u].size(); i++){
if(g[u][i].to == fa) continue;
dfs(g[u][i].to,u,limit);
if(g[u][i].w <= limit)
dp[u] += min(dp[g[u][i].to],g[u][i].w);
else dp[u] += dp[g[u][i].to];
}
if(g[u].size() == && g[u][].to == fa) dp[u] = INF;
}
int main() {
int i,j,u,v,w,lt,rt,mid,ans;
while(scanf("%d %d",&n,&m),n||m){
for(i = ; i <= n; i++)
g[i].clear();
for(lt = rt = ,i = ; i < n; i++){
scanf("%d %d %d",&u,&v,&w);
g[u].push_back(arc(v,w));
g[v].push_back(arc(u,w));
if(w > rt) rt = w;
}
ans = -;
while(lt <= rt){
mid = (lt+rt)>>;
memset(dp,,sizeof(dp));
dfs(,-,mid);
if(dp[] <= m){
ans = mid;
rt = mid-;
}else lt = mid+;
}
printf("%d\n",ans);
}
return ;
}
 

最新文章

  1. 双系统Ubuntu分区扩容过程记录
  2. java servlet手机app访问接口(四)推送
  3. VS2010最常用快捷键
  4. QT 智能提示设置
  5. 【Bootstrap3.0建站笔记三】AspNetPager分页,每一列都可排序
  6. jQuery中的trigger和triggerhandler区别
  7. Copy from chromium-dev!
  8. 使用Maven打包项目并上传到Linux服务器
  9. 解决浏览器兼容问题的css hack
  10. marble 基本函数(一)
  11. ds18b20再硬件设计部分的注意事项
  12. 解决 PHPExcel 长数字串显示为科学计数[转]
  13. python3+Robot Framework+PyCharm第一个WEB UI自动化用例
  14. 由AbstractQueuedSynchronizer和ReentrantLock来看模版方法模式
  15. yarn 常用命令
  16. python第三十二课——队列
  17. mfc CTabCtrl
  18. ASP.NET MVC:如何提供 Controller 继承体系使用的 ModelBinder?
  19. Linxu磁盘分区
  20. Spark任务提交底层原理

热门文章

  1. Sqlite数据库中索引的使用、索引的优缺点[转]
  2. 2、IO流的分类和IO流体系
  3. C. Mahmoud and a Message dp + 暴力
  4. Matrix Transformation codechef 数学题
  5. JDK11源码分析之集合类(一)----HashMap
  6. JS filters-table简单过滤-Version 1
  7. 解决webstromm标签高亮问题
  8. R Programming week 3-Loop functions
  9. ECharts Map 属性详解
  10. 迅为i.MX6UL核心板ARMCortex-A7单核NXP飞思卡尔工控行业Imx6核心板