sicily 1019. Apple Tree

来源:互联网 发布:k3即时库存 sql 编辑:程序博客网 时间:2024/06/05 06:30

1019. Apple Tree

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

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.

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 111 23 20 1 21 21 3

Sample Output

112

Problem Source

ZSUACM Team Member

今天终于解决了这道题,甚是不容易,不想多说,看注释。

#include<bits/stdc++.h>#define maxn 250using namespace std;int n,k;int l,r;int apple[maxn];vector<int> adj[maxn];int dp[maxn][maxn][2];//一定要加访问数组,否则子节点会搜索到父节点 bool vis[maxn];void search(int cur,int depth){    vis[cur] = true;    int len = adj[cur].size();    //这句加不加无所谓    if(depth==k)return;        for(int i=0;i<len;i++){        int son = adj[cur][i];        if(!vis[son]){            search(son,depth+1);            //决策父节点还剩j步的情况             for(int j=k-depth;j>=0;j--){                int max0 = -1;                int max1 = -1;                //此处j1表示父节点能给子节点用的步数                for(int j1=j;j1>=1;j1--) {                 //分2步以上给子节点,能返回父节点                     if(j1>=2){                        max0 = max(max0,dp[cur][j-j1][0]+dp[son][j1-2][0]);                        max1 = max(max1,dp[cur][j-j1][1]+dp[son][j1-2][0]);                    }                //也可能不会返回                    max1 = max(max1,dp[cur][j-j1][0]+dp[son][j1-1][1]);                 }                 dp[cur][j][0] = max(dp[cur][j][0],max0);                dp[cur][j][1] = max(dp[cur][j][1],max1);            }        }     }}int main(){    while(~scanf("%d%d",&n,&k)){        memset(vis,0,sizeof(vis));        memset(dp,0,sizeof(dp));        for(int i=0;i<maxn;i++)        adj[i].clear();        for(int i=0;i<n;i++){            scanf("%d",&apple[i]);            //初始化很重要,表示每一个节点无论剩余多少步一开始都是节点本身的苹果数             for(int i=0;i<n;++i)                for(int j=0;j<=k;++j)                    dp[i][j][0]=dp[i][j][1]=apple[i];        }        for(int i=0;i<n-1;i++){            scanf("%d%d",&l,&r);            adj[l-1].push_back(r-1);            adj[r-1].push_back(l-1);        }        //从节点0开始,深度是0        search(0,0);        printf("%d\n",max(dp[0][k][0],dp[0][k][1]));    }}                                 


0 0