sicily 1019. Apple Tree

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 11
1 2
3 2
0 1 2
1 2
1 3

Sample Output

11
2

Problem Source

ZSUACM Team Member

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

#include<bits/stdc++.h>
#define maxn 250
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值