poj2486Apple Tree[樹形揹包!!!]

Candy?發表於2016-09-27
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+5,K=2e2+5,INF=1e9+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int n=0,k,u,v,w[N];
struct edge{
    int v,ne;
}e[N<<1];
int h[N],cnt=0;
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][2];
void dp(int u,int fa){
    for(int i=0;i<=k;i++)d[u][i][0]=d[u][i][1]=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>=1;j--){
            for(int z=1;z<=j;z++){
                if(z>=2) d[u][j][0]=max(d[u][j][0],d[u][j-z][0]+d[v][z-2][0]);
                if(z>=1) d[u][j][1]=max(d[u][j][1],d[u][j-z][0]+d[v][z-1][1]);
                if(z>=2) d[u][j][1]=max(d[u][j][1],d[u][j-z][1]+d[v][z-2][0]);
            }
            //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=0;memset(h,0,sizeof(h));
        for(int i=1;i<=n;i++) w[i]=read();
        for(int i=1;i<=n-1;i++){u=read();v=read();ins(u,v);}
        //memset(f,0,sizeof(f));
        dp(1,-1);
        printf("%d\n",d[1][k][1]);
        
       // cout<<"\n\n";
       // for(int i=1;i<=n;i++ ) printf("son %d %d\n",i,son[i]);
    }
}

 

相關文章