poj2486Apple Tree[树形背包!!!]
Posted Candy?
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj2486Apple Tree[树形背包!!!]相关的知识,希望对你有一定的参考价值。
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.
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:[email protected]
题意:从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]); } }
以上是关于poj2486Apple Tree[树形背包!!!]的主要内容,如果未能解决你的问题,请参考以下文章