首页 购物 网址 三丰软件 | 小说 美女秀 图库大全 游戏 笑话 | 下载 开发知识库 新闻 开发 图片素材
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
移动开发 架构设计 编程语言 Web前端 互联网
开发杂谈 系统运维 研发管理 数据库 云计算 Android开发资料
资讯 业界资讯 软件杂谈 编程开发 网站建设 网络观查 搜索引擎 移动应用 网站运营 网络地图
开发 移动开发 Web前端 架构设计 编程语言 互联网 数据库 系统运维 云计算 开发杂谈
[编程语言] POJ2486---Apple Tree
POJ2486---Apple Tree

Apple Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7598   Accepted: 2548

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
树形dp
设dp[i][j][0] 表示 在以i为根的子树下走了j步最后不回到i,能够得到的最多苹果数
dp[i][j][1] 表示 在以i为根的子树下走了j步最后回到i,能够得到的最多苹果数
设u为某子树根,v为其中一个儿子节点
dp[u][i][1] = max(dp[u][i][1], dp[v][j - 2][1] + dp[u][i - j][1] + apple[v]);
dp[u][i][0] = max(dp[u][i][0], dp[u][i - j][1] + dp[v][j - 1][0] + apple[v]);
dp[u][i][0] = max(dp[u][i][0], dp[u][i - j][0] + dp[v][j - 2][1] + apple[v]);
/*************************************************************************
    > File Name: POJ2486.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月02日 星期五 11时30分01秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

vector <int> edge[110];
int dp[110][220][2];
int apple[110];
int n, m;

void dfs(int u, int fa)
{
	int size = edge[u].size();
	for (int i = 0; i < size; ++i)
	{
		int v = edge[u][i];
		if (v == fa)
		{
			continue;
		}
		dfs(v, u);
		for (int j = m; j >= 1; --j)
		{
			for (int k = 1; k <= j; ++k)
			{
				if (k >= 2)
				{
					dp[u][j][1] = max(dp[u][j][1], dp[u][j - k][1] + dp[v][k - 2][1] + apple[v]);
					dp[u][j][0] = max(dp[u][j][0], dp[v][k - 2][1] + dp[u][j - k][0] + apple[v]);
				}
				dp[u][j][0] = max(dp[u][j][0], dp[u][j - k][1] + dp[v][k - 1][0] + apple[v]);
			}
		}
	}
}

int main()
{
	int u, v;
	while (~scanf("%d%d", &n, &m))
	{
		memset (dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i)
		{
			edge[i].clear();
			scanf("%d", &apple[i]);
		}
		for (int i = 1; i <= n - 1; ++i)
		{
			scanf("%d%d", &u, &v);
			edge[u].push_back(v);
			edge[v].push_back(u);
		}
		dfs(1, -1);
		printf("%d\n", dp[1][m][0] + apple[1]);
	}
	return 0;
}



 此文从网络中自动搜索生成,不代表本网站赞成被搜索网站的内容或立场    查看原文
360图书馆 软件开发资料 文字转语音 购物精选 软件下载 美食菜谱 新闻资讯 电影视频 小游戏 Chinese Culture 股票 租车
生肖星座 三丰软件 视频 开发 短信 中国文化 网文精选 搜图网 美图 阅读网 多播 租车 短信 看图 日历 万年历 2018年7日历
2018-7-22 11:23:19
 
  网站联系 软件世界网-www.sjsjw.com ©2014 蜀ICP备06016416号 三峰网旗下网站