HDU 4616 Game 树形DP
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.
Output
For each testcase, output the maximum total value of gifts you can get.
Sample Input
2 3 1 23 0 12 0 123 1 0 2 2 1 3 2 23 0 12 0 123 1 0 2 2 1
Sample Output
146 158
Author
SYSU
Source
2013 Multi-University Training Contest 2
給定一棵樹,每一個節點有一個權值,有的節點可能存在陷阱,經過一個頂點可以能獲得權值但也經歷陷阱。找出一條路徑,起點自選,
使在經歷陷阱數不超過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]代替的。
總結
以上是生活随笔為你收集整理的HDU 4616 Game 树形DP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 企业直播软件对比
- 下一篇: 无向图双连通分量BCC(全网最好理解)