標籤:poj 樹形dp
Apple Tree
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 7784 |
|
Accepted: 2603 |
Description
Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.
Input
There are several test cases in the input
Each test case contains three parts.
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)
The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i.
The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent.
Input will be ended by the end of file.
Note: Wshxzt starts at Node 1.
Output
For each test case, output the maximal numbers of apples Wshxzt can eat at a line.
Sample Input
2 1 0 111 23 20 1 21 21 3
Sample Output
112
Source
POJ Contest,Author:[email protected]
題目連結:http://poj.org/problem?id=2486
題目大意:有一棵n個結點的蘋果樹,每個結點上有一些蘋果,現在從結點1開始走m步,問最多可以吃多少蘋果,一個結點的蘋果被吃光後就沒有了
題目分析:弱渣做了好久啊,題意很好理解,關鍵是可能會回頭,所以我們列狀態的時候要考慮回頭,不回頭兩種,即
dp[u][i][0] 表示從結點u出發走了i步最後不回到0時吃掉的蘋果數
dp[u][i][1] 表示從結點u出發走了i步最後回到0時吃掉的蘋果數
分三種情況:
u返回 v返回:u到別的子樹後回到u再到v子樹,遍曆完v子樹,回到v再回到u, u -> v, v -> u多出2步
u返回 v不返回:u到別的子樹後回到u再到v子樹,然後一直向下遍曆v子樹 u -> v多出1步
u不返回 v返回:u遍曆完v子樹再回到v再回到u再去遍曆u的其他子樹 u -> v, v -> u多出2步
狀態轉移方程:
dp[u][j + 2][1] = max(dp[u][j + 2][1], dp[u][k][1] + dp[v][j - k][1])
dp[u][j + 1][0] = max(dp[u][j + 1][0], dp[u][k][1] + dp[v][j - k][0])
dp[u][j + 2][0] = max(dp[u][j + 2][0], dp[u][k][0] + dp[v][j - k][1])
vector:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;int const MAX = 205;vector <int> t[MAX];int dp[MAX][MAX][2], val[MAX];int n, m;bool vis[MAX];void DFS(int u){ vis[u] = true; for(int i = 0; i <= m; i++) dp[u][i][0] = dp[u][i][1] = val[u]; //從u出發至少可以獲得val[u]個 int len = t[u].size(); for(int i = 0; i < len; i++) { int v = t[u][i]; if(!vis[v]) { DFS(v); for(int j = m; j >= 0; j--) { for(int k = 0; k <= j; k++) { dp[u][j + 2][1] = max(dp[u][j + 2][1], dp[u][k][1] + dp[v][j - k][1]); dp[u][j + 1][0] = max(dp[u][j + 1][0], dp[u][k][1] + dp[v][j - k][0]); dp[u][j + 2][0] = max(dp[u][j + 2][0], dp[u][k][0] + dp[v][j - k][1]); } } } }}int main(){ while(scanf("%d %d", &n, &m) != EOF) { memset(dp, 0, sizeof(dp)); memset(vis, false, sizeof(vis)); for(int i = 1; i <= n; i++) { scanf("%d", &val[i]); t[i].clear(); } for(int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); t[u].push_back(v); t[v].push_back(u); } DFS(1); printf("%d\n", dp[1][m][0]); }}
數組構樹:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 205;int dp[MAX][MAX][2], val[MAX];int head[MAX], cnt;int n, m;struct Edge{ int to, next;}e[MAX * MAX / 2];void Add(int x, int y){ e[cnt].to = y; e[cnt].next = head[x]; head[x] = cnt++;}void DFS(int u, int fa){ for(int i = 0; i <= m; i++) dp[u][i][0] = dp[u][i][1] = val[u]; for(int i = head[u]; i != -1; i = e[i].next) { int v = e[i].to; if(v != fa) { DFS(v, u); for(int j = m; j >= 0; j--) { for(int k = 0; k <= j; k++) { dp[u][j + 2][1] = max(dp[u][j + 2][1], dp[u][k][1] + dp[v][j - k][1]); dp[u][j + 1][0] = max(dp[u][j + 1][0], dp[u][k][1] + dp[v][j - k][0]); dp[u][j + 2][0] = max(dp[u][j + 2][0], dp[u][k][0] + dp[v][j - k][1]); } } } }}int main(){ while(scanf("%d %d", &n, &m) != EOF) { cnt = 0; memset(dp, 0, sizeof(dp)); memset(head, -1, sizeof(head)); for(int i = 1; i <= n; i++) scanf("%d", &val[i]); for(int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); Add(u, v); Add(v, u); } DFS(1, -1); printf("%d\n", dp[1][m][0]); }}
POJ 2486 Apple Tree (樹形dp 經典題)