[HDU3586]Information Disturbing(DP + 二分)

传送门

二分答案,再 DP,看看最终结果是否小于 m

——代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define N 1001
 5 #define max(x, y) ((x) > (y) ? (x) : (y))
 6
 7 int n, m, cnt, ans;
 8 int a[N], sum[N], head[N], to[N << 1], val[N << 1], next[N << 1];
 9 bool vis[N];
10
11 inline int read()
12 {
13     int x = 0, f = 1;
14     char ch = getchar();
15     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
16     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
17     return x * f;
18 }
19
20 inline void add(int x, int y, int z)
21 {
22     to[cnt] = y;
23     val[cnt] = z;
24     next[cnt] = head[x];
25     head[x] = cnt++;
26 }
27
28 inline void dfs(int u, int x)
29 {
30     int i, v;
31     vis[u] = 1;
32     for(i = head[u]; i ^ -1; i = next[i])
33     {
34         v = to[i];
35         if(!vis[v])
36         {
37             dfs(v, x);
38             if(val[i] <= x)
39             {
40                 if(val[i] < sum[v]) sum[u] += val[i];
41                 else sum[u] += sum[v];
42             }
43             else sum[u] += sum[v];
44         }
45     }
46 }
47
48 inline bool check(int x)
49 {
50     memset(vis, 0, sizeof(vis));
51     memset(sum, 0, sizeof(sum));
52     for(int i = 2; i <= n; i++)
53         if(a[i] == 1)
54             sum[i] = m + 1;
55     dfs(1, x);
56     return sum[1] <= m;
57 }
58
59 int main()
60 {
61     int i, x, y, z, mid;
62     while(1)
63     {
64         cnt = 0;
65         n = read();
66         m = read();
67         if(!n && !m) break;
68         memset(a, 0, sizeof(a));
69         memset(head, -1, sizeof(head));
70         for(i = 1; i < n; i++)
71         {
72             a[x = read()]++;
73             a[y = read()]++;
74             z = read();
75             add(x, y, z);
76             add(y, x, z);
77         }
78         x = 1, y = m, ans = -1;
79         while(x <= y)
80         {
81             mid = (x + y) >> 1;
82             if(check(mid)) ans = mid, y = mid - 1;
83             else x = mid + 1;
84         }
85         printf("%d\n", ans);
86     }
87     return 0;
88 }

时间: 2024-10-24 14:29:07

[HDU3586]Information Disturbing(DP + 二分)的相关文章

[hdu3586]Information Disturbing树形dp+二分

题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到ll,如果用设inf的解法. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=1e6+7; 5 const int inf=0x3f3f3f3f; 6 struct e

hdu3586 Information Disturbing[二分答案+树形DP]

给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足这个前提下要求切断的边权的最大值最小,求出这个最小值.$n ≤ 10^5$ 首先这个最大值肯定二分答案,然后树形DP限制割掉的边不能超过这个二分的边权,设$f[i]$表示在这个限制下该子树内所有叶子断绝与根的联系的最小代价. 于是$f[i]=max(w_{father},\sum\limits_{y

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

题目大意:一棵有n个节点的有根树,1为根节点,边带权,表示删掉这条边的代价.现在要删掉一些边,使叶子节点不能到达根节点.但是,每次删除的边的代价不能超过limit,删掉的边的总代价不能超过m,求最小的limit的可能取值. 题目分析:二分枚举limit,定义状态dp(u)表示将u与它管辖的叶子节点失去联系所需要的总代价,则: dp(u)+=min(dp(son),e[i].w),e[i].w<=limit: dp(u)+=dp(son)  e[i].w>limit: 代码如下: # inclu

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

http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超过上限limit,问在保证总费用<=m下的最小的limit. 思路: 对于上限limit我们可以二分查找.然后就是树形dp,看代码就可以理解的. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring>

HDU 3586 Information Disturbing 树形DP+二分

Information Disturbing 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 communicati

hdu 3586 Information Disturbing (树形dp)

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 1722    Accepted Submission(s): 641 Problem Description In the battlefield , an effective way to defeat enemies is to bre

HDU 3586.Information Disturbing 树形dp

Information Disturbing Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3205    Accepted Submission(s): 1137 Problem Description In the battlefield , an effective way to defeat enemies is to bre

hdu3586 树形dp+二分求解

http://acm.hdu.edu.cn/showproblem.php?pid=3586 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 w

BNUOJ 7697 Information Disturbing

Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 358664-bit integer IO format: %I64d      Java class name: Main In the battlefield , an effective way to defeat enemies is to break their