hdu3586 Information Disturbing(树形dp)
来源:互联网 发布:brew install php扩展 编辑:程序博客网 时间:2024/06/05 14:27
hdu3586
题目
就是给你一棵树,1节点是指挥部,叶子节点是前线士兵,每条边除去有代价,但代价的总和要小于上限m,现要让指挥部和叶子节点失去联系,要使删去的边中最大的最小,问这个最小值是多少。
思路
首先要想到这是一个二分判定性问题,二分上限值,用树形dp进行可行性判断dp[i]表示选i节点花费的最小代价的总和,dp[i]要么加上和子节点相连的那条边,或者是子节点的dp,注意子节点要赋成inf,且wa的可能是inf太大了。。。(竟然二分都要不会写了23333)
代码
#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <iostream>#include <stack>#include <queue>using namespace std;typedef long long ll;const int maxn=1100;const int maxm=1000100;int tot,head[maxn],n,m;struct node{ int next; int to; int w;} edge[maxn*2];void addedge(int from,int to,int w){ edge[tot].to=to; edge[tot].next=head[from]; edge[tot].w=w; head[from]=tot++;}int dp[maxn];int lim;void dfs(int u,int fa){ int cnt=0; dp[u]=0; for(int i=head[u]; ~i; i=edge[i].next) { int v=edge[i].to; int w=edge[i].w; if(v==fa) continue; cnt++; dfs(v,u); if(w<=lim) dp[u]+=min(w,dp[v]); else { dp[u]+=dp[v]; } } if(cnt==0) { dp[u]=maxm; }}int solve(){ memset(dp,0,sizeof(dp)); dfs(1,-1); if(dp[1]>m) return 0; return 1;}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { if(n==0&&m==0) break; int maxe=0; memset(head,-1,sizeof(head)); tot=0; for(int i=0; i<n-1; i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); addedge(a,b,c); addedge(b,a,c); maxe=max(maxe,c); } int l=1; int r=maxe; int mid=(l+r)>>1; int ans=maxm; while(l<=r) { mid=(l+r)>>1; lim=mid; if(solve()) { r=mid-1; ans=mid; } else l=mid+1; } if(ans==maxm) printf("-1\n"); else printf("%d\n",ans); } return 0;}
0 0
- 树形DP+二分(Information Disturbing HDU3586)
- hdu3586 Information Disturbing(树形dp)
- HDU3586 Information Disturbing(二分+树形dp)
- 【树形DP】 hdu3586 Information Disturbing
- hdu3586 Information Disturbing 树形DP
- 【树形DP+二分】HDU3586 Information Disturbing
- 【HDU3586】Information Disturbing-二分答案+树形DP
- HDU3586 Information Disturbing 解题报告【树形DP】
- hdu3586 Information Disturbing(树状DP+二分查找)
- HDU 3586 Information Disturbing(树形DP)
- HDU 3586 Information Disturbing(树形DP)
- hdu 3586 Information Disturbing(树形dp)
- hdu 3586 Information Disturbing(树形dp)
- Information Disturbing (树形dp+二分)
- hdu3586(树形dp)
- hdu 3586 Information Disturbing(树形dp + 二分)
- hdu 3586 Information Disturbing (树形dp+二分)
- HDU 3586 - Information Disturbing(二分+树形DP)
- 派生类有参构造函数的编程
- Java集合类的总结与比较
- Swift中tableView加载通过xib创建的headerView时,无法显示xib视图内容.
- 二维动态数组申请(中兴笔试后)
- iOS 利用Javascript去操作UIWebView的内容
- hdu3586 Information Disturbing(树形dp)
- require js之define 函数
- python 字典(dict)遍历四种方法的性能
- Openjudge 2.2基本算法之递归和自调用函数 2705:括号匹配问题
- Python的struct
- loading加载动画
- [leetcode] 121. Best Time to Buy and Sell Stock
- 可以拖动的 view
- c++ 在console中控制光标的位置