码迷,mamicode.com
首页 > 移动开发 > 详细

poj-2486-Apple Tree

时间:2017-11-18 21:04:53      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:div   ssi   not   day   ref   return   scanf   from   pig   

poj-2486-Apple Tree

 

Apple Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11210   Accepted: 3774

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

 

 

17857578   2486 Accepted 892K 63MS G++ 1191B 2017-11-18 19:26:17

 

树状DP。

本题的关键在于建立dp转化方程。

  dp[x][j][0] = max( dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0] );

  表示的是不回到当前x点的话,是回到j - t 步的x当前点 加上 不回到 y 点的步骤

 
  dp[x][j][0] = max(dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1] );

  表示的 不回到当前x点。 是 不回到当前 x 点 和 回到y点的 之和。


  dp[x][j][1] = max(dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1] );

  表示回到当前x点, 是回到x点和回到子结点y的总和。

 

// poj-2486  

#include <cstdio> 
#include <cstring> 


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

const int MAXN = 100 + 10; 

int n, k, val[MAXN], vis[MAXN], dp[MAXN][2*MAXN][2]; 

vector<int> vt[MAXN]; 

void dfs(int x){
	vis[ x ] = 1;  
	for(int i=0; i<vt[x].size(); ++i){
		int y = vt[x][i]; 
		if(vis[ y ] == 1){
			continue; 
		} 
		dfs(y); 
		for(int j=k; j>=1; --j){
			for(int t=1; t<=j; ++t){
				dp[x][j][0] = max( dp[x][j][0], dp[x][j-t][1] + dp[y][t-1][0] ); 
				if(t >= 2){
					dp[x][j][0] = max(dp[x][j][0], dp[x][j-t][0] + dp[y][t-2][1] ); 
					dp[x][j][1] = max(dp[x][j][1], dp[x][j-t][1] + dp[y][t-2][1] ); 
				}
			}
		} 
	}
}

int main(){
	freopen("in.txt", "r", stdin); 

	int a, b, ans;  
	while(scanf("%d %d", &n, &k) != EOF){
		memset(dp, 0, sizeof(dp)); 
		memset(vis, 0, sizeof(vis)); 

		for(int i=1; i<=n; ++i){
			scanf("%d", &val[i]); 

			for(int j=0; j<=k; ++j){
				dp[i][j][0] = dp[i][j][1] = val[i]; 
			}
		}

		for(int i=1; i<=n; ++i){
			vt[i].clear(); 
		}

		for(int i=1; i<n; ++i){
			scanf("%d %d", &a, &b); 
			vt[a].push_back(b); 
			vt[b].push_back(a); 
		} 

		dfs(1); 

		ans = max(dp[1][k][0], dp[1][k][1]); 

		printf("%d\n", ans );
	} 
	return 0; 
}

  

 

poj-2486-Apple Tree

标签:div   ssi   not   day   ref   return   scanf   from   pig   

原文地址:http://www.cnblogs.com/zhang-yd/p/7857718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!