poj2486Apple Tree[树形背包!!!]

时间:2021-02-02 08:22:32
Apple Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9989   Accepted: 3324

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

题意:从1开始走,点有权,问k步之内最大值

典型树形背包,每个点容量为k的背包每个字节点j是一个组每个组k个物品
问题在于,如果想到别的孩子去,还要回到根
d[i][j][0/1]表示子树i走j步回到根/不回到根的最大值
转移分三个:d[i][j][0]一种,d[i][j][1]两种:可以是当前孩子不回来,也可以是当前孩子回来
 
一些细节:
1.不一定走k步,先把所有容量都装上自己w[u]
2.这样的话不要用son了,容量都用k
//
// main.cpp
// poj2486
//
// Created by Candy on 9/27/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e2+,K=2e2+,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n=,k,u,v,w[N];
struct edge{
int v,ne;
}e[N<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int d[N][K][];
void dp(int u,int fa){
for(int i=;i<=k;i++)d[u][i][]=d[u][i][]=w[u];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(v==fa) continue;
dp(v,u);
for(int j=k;j>=;j--){
for(int z=;z<=j;z++){
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
if(z>=) d[u][j][]=max(d[u][j][],d[u][j-z][]+d[v][z-][]);
}
//printf("d %d %d %d %d\n",u,j,d[u][j][0],d[u][j][1]);
}
}
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
cnt=;memset(h,,sizeof(h));
for(int i=;i<=n;i++) w[i]=read();
for(int i=;i<=n-;i++){u=read();v=read();ins(u,v);}
//memset(f,0,sizeof(f));
dp(,-);
printf("%d\n",d[][k][]); // cout<<"\n\n";
// for(int i=1;i<=n;i++ ) printf("son %d %d\n",i,son[i]);
}
}