您的位置:首页 > 移动开发

poj 2486 Apple Tree(DP-树形DP)

2014-08-01 22:18 513 查看
Apple Tree

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 7114Accepted: 2366
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 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output
11
2

Source
POJ Contest,Author:magicpig@ZSU

dp[100][200][2]
dp[i][k][0]表示以i为起点,向子树方向走k步,并且最终未停留在i点,吃到最多苹果数
dp[i][k][1]表示以i为起点,向子树方向走k步,并且最终停留在i点,吃到最多苹果数

则,可推出下列公式:
dp[f][k+m+2][1] = max(dp[f][k][1]+dp[s][m][1] , dp[f][k+m+2][1]);

dp[f][k+m+2][0] = max(dp[f][k][0]+dp[s][m][1] , dp[f][k+m+2][0]);

dp[f][k+m+1][0] = max(dp[f][k][1]+dp[s][m][0] , dp[f][k+m+1][0]);

dp[f][k+m+1][0] = max(dp[f][k][1]+dp[s][m][1] , dp[f][k+m+1][0]);

f为父亲节点,s为儿子节点

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;

const int maxn = 110;
int dp[maxn][2*maxn][2];
int N , K;
vector<int> v[maxn];

void initial(){
for(int i = 0; i < maxn; i++){
v[i].clear();
for(int j = 0; j < 2*maxn; j++){
dp[i][j][0] = 0;
dp[i][j][1] = 0;
}
}
}

void readcase(){
for(int i = 1; i <= N; i++) scanf("%d" , &dp[i][0][1]);
int a , b;
for(int i = 0; i < N-1; i++){
scanf("%d%d" , &a , &b);
v[a].push_back(b);
v[b].push_back(a);
}
}

void DP(int n , int f){
if(v
.size() == 1 && v
[0] == f) return;
for(int i = 0; i < v
.size(); i++){
int s = v
[i];
if(s != f){
DP(s , n);
for(int k = K-1; k >= 0; k--){
for(int m = 0; m+k+1 <= K; m++){
dp
[k+m+2][1] = max(dp
[k][1]+dp[s][m][1] , dp
[k+m+2][1]);
dp
[k+m+2][0] = max(dp
[k][0]+dp[s][m][1] , dp
[k+m+2][0]);
dp
[k+m+1][0] = max(dp
[k][1]+dp[s][m][0] , dp
[k+m+1][0]);
dp
[k+m+1][0] = max(dp
[k][1]+dp[s][m][1] , dp
[k+m+1][0]);
}
}
}
}
}

void computing(){
DP(1 , 0);
int ans = 0;
for(int i = 0; i <= K; i++){
ans = max(ans , dp[1][i][0]);
ans = max(ans , dp[1][i][1]);
}
cout << ans << endl;
}

int main(){
while(cin >> N >> K){
initial();
readcase();
computing();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: