HDU 3586 树状dp

Information Disturbing

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

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

Author

alpc86

Source

2010 ACM-ICPC Multi-University Training Contest(15)——Host by NUDT

HDU 3586:

题目意思;

打断一棵树的叶子节点和根节点之间的联系,要求只有士兵的能力大于这条边所需要的能力时才能被打断,同时满足所有消耗的能力值之和不大于m,求士兵所具有的最小的能力值;

解题思路:

首先对士兵的能力值在一个区间内进行二分,然后对于每一个值进行dp,状态转移方程为:

if(dp[son]>cost[father][son]&&cost[father][cost]<=mid)

dp[son]=cost[father][son];

然后求到根节点的时候,把根节点的儿子节点的所有dp值都加起来如果小于等于m,则返回真值,否则,返回0;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<vector>
 7 using namespace std;
 8 const  int maxn=2010;
 9 const int inf=1000001;
10 vector<int> G[maxn];
11 int cost[maxn][maxn];
12 int n,m,head,mid;
13 int dfs(int parent,int point)
14 {
15     int ans=0;
16     for(int i=0;i<G[point].size();i++)
17     {
18          if(G[point][i]==parent) continue;
19          int result=dfs(point,G[point][i]);
20          if(cost[point][G[point][i]]<=result&&cost[point][G[point][i]]<=mid)
21          {
22              result=cost[point][G[point][i]];
23          }
24          ans+=result;
25     }
26     if(!ans) return inf;
27     return ans;
28 }
29 int main()
30 {
31  //  freopen("in.txt","r",stdin);
32     int u,v,c;
33     while(~scanf("%d%d",&n,&m)){
34         if(!n&&!m) break;
35         int l=inf,r=0;
36         for(int i=0;i<maxn;i++) while(!G[i].empty()) G[i].pop_back();
37         for(int i=0;i<n-1;i++)
38         {
39             scanf("%d%d%d",&u,&v,&c);
40             if(c<l) l=c;
41             if(c>r) r=c;
42             G[u].push_back(v);
43             G[v].push_back(u);
44         cost[u][v]=cost[v][u]=c;
45         }
46         int ans=-1;
47         while(l<=r){
48             mid=(l+r)>>1;
49             if(dfs(0,1)<=m){
50                 ans=mid;
51                 r=mid-1;
52             }
53             else l=mid+1;
54         }
55          printf("%d\n",ans);
56     }
57     return 0;
58 }
时间: 2024-10-20 13:15:15

HDU 3586 树状dp的相关文章

HDU 2196 树状dp 求树中节点之间的最长距离

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3749    Accepted Submission(s): 1892 Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du

HDU 4035Maze(树状+概率dp,绝对经典)

题意: 给你n个节点的树,从1节点开始走,到每个节点都有三种情况,被杀死回到1节点,找到隐藏的出口出去,沿着当前节点相邻的边走到下一个节点,给出每个节点三种情况发生的概率分别为ki,ei,1-ki-ei,求找到出口时已经过的边数的期望. 分析: 用树状dp考虑问题.当节点是叶子节点时它只是向父节点走,非叶子节点可以向父亲节点和所有孩子节点走. Ei表示到i节点经过边数的期望 则叶子节点:Ei=ki*E1+(1-ki-ei)*(E[par[i]]+1);//par[i]表示i的父亲节点 非叶子节点

树状DP入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题目大意:给定一棵关系树,每个节点有个权值,子节点和父节点不能同时选,问最后能选的最大价值是多少? 解题思路:树形DP入门题.由于子节点与父节点不能同时选,有人可能会用贪心思想,二者选其一肯定最优.其实不然,有可能父节点和子节点都不选,而要选子孙节点.不过只要再往深点想下,就可以得出动态规划的解法.每个节点要么选要么不选,和大多数选不选动归一样,来个dp[i][2],0表示不选,1表示不选,那

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

hdu 3333 树状数组+离线处理

http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了很久不知道怎么处理,而且答案没看懂,然后找个例子模拟下别人的代码马上懂了---以后看不懂的话就拿个例子模拟下别人的代码 举个例子:1 3 3 5 3 5 查询 a, 2 4 b, 2 5 最初是这么想的:对于a查询,倘若把第二个数第三个数变成1个3,那么到b查询,又出现了两个3,再做处理似乎还是O(n),而且如果先出现2,5查询,后出现2,4查询,那么还需要把删除的数补回来.....o(╯

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

洛谷P1122 最大子树和 (树状dp)

题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明就向老师提出了这个问题: 一株奇怪的花卉,上面共连有N 朵花,共有N-1条枝干将花儿连在一起,并且未修剪时每朵花都不是孤立的.每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美丽指数”为负 数的,说明这朵花看着都让人恶心.所谓“修剪”,意为:去掉其中的一条枝条,这样一株花就成了两株,扔掉

POJ 1463 树状dp

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6629   Accepted: 3058 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

POJ 2342 树状dp

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4606   Accepted: 2615 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure