首页 购物 网址 三丰软件 | 小说 美女秀 图库大全 游戏 笑话 | 下载 开发知识库 新闻 开发 图片素材
 俄罗斯方块 ↓俄罗斯方块↓ TxT小说阅读器 ↓语音阅读,小说下载,古典文学↓ 一键清除垃圾 ↓轻轻一点,清除系统垃圾↓ 图片批量下载器 ↓批量下载图片,美女图库↓ 移动开发 架构设计 编程语言 Web前端 互联网 开发杂谈 系统运维 研发管理 数据库 云计算 Android开发资料
 资讯 业界资讯 软件杂谈 编程开发 网站建设 网络观查 搜索引擎 移动应用 网站运营 网络地图 开发 移动开发 Web前端 架构设计 编程语言 互联网 数据库 系统运维 云计算 开发杂谈
 [编程语言] ＰＯＪ２４８６－－－Apple Tree
 ＰＯＪ２４８６－－－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[i][j] 表示　在以ｉ为根的子树下走了ｊ步最后回到ｉ，能够得到的最多苹果数

dp[u][i] = max(dp[u][i], dp[v][j - 2] + dp[u][i - j] + apple[v]);
dp[u][i] = max(dp[u][i], dp[u][i - j] + dp[v][j - 1] + apple[v]);
dp[u][i] = max(dp[u][i], dp[u][i - j] + dp[v][j - 2] + 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;
int dp;
int apple;
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] = max(dp[u][j], dp[u][j - k] + dp[v][k - 2] + apple[v]);
dp[u][j] = max(dp[u][j], dp[v][k - 2] + dp[u][j - k] + apple[v]);
}
dp[u][j] = max(dp[u][j], dp[u][j - k] + dp[v][k - 1] + 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[m] + apple);
}
return 0;
}```

 此文从网络中自动搜索生成，不代表本网站赞成被搜索网站的内容或立场    查看原文