hdu4616 树形dp(有限制的权值最大链)
来源:互联网 发布:证大财富淘宝贷假不假 编辑:程序博客网 时间:2024/04/30 11:50
http://acm.hdu.edu.cn/showproblem.php?pid=4616
Problem Description
Nowadays, there are more and more challenge game on TV such as 'Girls, Rush Ahead'. Now, you participate int a game like this. There are N rooms. The connection of rooms is like a tree. In other words, you can go to any other room by one and only one way. There is a gift prepared for you in Every room, and if you go the room, you can get this gift. However, there is also a trap in some rooms. After you get the gift, you may be trapped. After you go out a room, you can not go back to it any more. You can choose to start at any room ,and when you have no room to go or have been trapped for C times, game overs. Now you would like to know what is the maximum total value of gifts you can get.
Input
The first line contains an integer T, indicating the number of testcases.
For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected.
All gifts' value are bigger than 0.
For each testcase, the first line contains one integer N(2 <= N <= 50000), the number rooms, and another integer C(1 <= C <= 3), the number of chances to be trapped. Each of the next N lines contains two integers, which are the value of gift in the room and whether have trap in this rooom. Rooms are numbered from 0 to N-1. Each of the next N-1 lines contains two integer A and B(0 <= A,B <= N-1), representing that room A and room B is connected.
All gifts' value are bigger than 0.
Output
For each testcase, output the maximum total value of gifts you can get.
Sample Input
23 123 012 0123 10 22 13 223 012 0123 10 22 1
Sample Output
146158
Author
SYSU
Source
2013 Multi-University Training Contest 2
/**hdu4616 树形dp(有限制的权值最大链)题目大意:给定一棵树,每一个节点有一个权值,有的节点可能存在陷阱,经过一个顶点可以能获得权值但也经历陷阱。找出一条路径,起点自选, 使在经历陷阱数不超过C的情况下,获得的最大的权值是多少?解题思路:一道很好的树形dp。如果把陷阱去掉,应该算一个经典的题目。其实加上一样:dp[u][j][k]表示到节点u经过j个陷阱,并要往根节点方向 或要往叶子节点方向去,所获得最大权值(1表示往叶子节点,0往根节点) 有一点值得一提:就是如果u点有陷阱,那么dp[u][1][1]不能由dp[v][0][1]转移过来,因为一旦限制为1,那么就不能由u往叶子方向走了 这一点很难理解。我们在求最大值是会用到dp[u][0][1]的,怎么办呢?我们发现dp[u][0][1]是可以由dp[u][0][0]代替的。*/#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=50050;int head[maxn],ip;int n,m,ans;void init(){ memset(head,-1,sizeof(head)); ip=0;}struct note{ int v,next;}edge[maxn*2];void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}int dp[maxn][5][2],gift[maxn],trap[maxn];void dfs(int u,int pre){ dp[u][trap[u]][0]=dp[u][trap[u]][1]=gift[u]; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; dfs(v,u); for(int j=0;j<=m;j++) { for(int k=0;k+j<=m;k++) { if(j!=m)ans=max(ans,dp[u][j][0]+dp[v][k][1]); if(k!=m)ans=max(ans,dp[u][j][1]+dp[v][k][0]); if(j+k<m)ans=max(ans,dp[u][j][0]+dp[v][k][0]); } } for(int j=0;j<=m;j++) { dp[u][j+trap[u]][0]=max(dp[u][j+trap[u]][0],dp[v][j][0]+gift[u]); if(j!=0) { dp[u][j+trap[u]][1]=max(dp[u][j+trap[u]][1],dp[v][j][1]+gift[u]); } } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++) { scanf("%d%d",&gift[i],&trap[i]); } init(); for(int i=0;i<n-1;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } ans=0; memset(dp,0,sizeof(dp)); dfs(0,-1); printf("%d\n",ans); } return 0;}
0 0
- hdu4616 树形dp(有限制的权值最大链)
- hdu4616(树形dp)
- 树形DP(HDU4616)
- hdu4616 树形DP
- 树形dp--hdu4616
- hdu4616(树形dp)
- hdu4616 树形dp
- hdu4616 Game 【树形dp】
- hdu4616 树形dp,分治思想
- HDU4616 Game (树形DP/搜索)
- HDU 4616 树形DP + 限制求最大贡献值的路
- HDU 5242Game 树上的贪心 树形dp 求出使K条链的权值总和最大
- hdu3534 树形dp(求树中两点之间的最大距离)
- 计蒜客: 最大的快乐指数(树形DP)
- 树形dp ,求最大路径权值,最长路径
- Codeforces 633 F The Chocolate Spree(树形dp,两条不相交链节点权值和最大)
- 最大独立子集模板!!!(树形dp)
- 最大利润问题(深搜+树形DP)
- parquet原理分析
- sizeof
- 【转】iPhone易被窃听应用三分钟即可获取所有信息--之文章
- JDK源码 -- Map篇
- [PHP]PHP函数
- hdu4616 树形dp(有限制的权值最大链)
- Apache Came组件rest的使用
- C++ <替换字符串中的数字>
- 越长大越孤单
- spring cron表达式
- 自有域名、IIS7,搭建SMTP邮件服务器
- 【黑马程序员】第三篇_关于I/O的总结
- printf 使用
- Android Studio为APP签名,提取签名的SHA1/MD5编码