標籤:
| Time Limit: 1000MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %lld & %llu |
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 111 23 20 1 21 21 3
Sample Output
112
Source
POJ Contest,Author:[email protected]
若當前走到了結點x,已經走了y步,除了走向子樹外,還有另一選擇:返回父節點,去父節點的其他子樹。
所以比一般的樹狀DP多加一維狀態,記錄有沒有返回當前結點。0表示要回,1表示不回。
懶得寫分析了,複製一份233
dp[root][j][0] = MAX (dp[root][j][0] , dp[root][j-k][0] + dp[son][k-2][0]);//從s出發,要回到s,需要多走兩步s-t,t-s,分配給t子樹k步,其他子樹j-k步,都返回
dp[root][j]][1] = MAX( dp[root][j][1] , dp[root][j-k][0] + dp[son][k-1][1]) ;//先遍曆s的其他子樹,回到s,遍曆t子樹,在當前子樹t不返回,多走一步
dp[root][j][1] = MAX (dp[root][j][1] , dp[root][j-k][1] + dp[son][k-2][0]);//不回到s(去s的其他子樹),在t子樹返回,同樣有多出兩步
by鍵盤上的舞者
我實際寫的時候0和1與上面說的相反。
1 /*by SilverN*/ 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 const int mxn=240; 9 struct edge{10 int v;11 int nxt;12 }e[mxn];13 int hd[mxn],mct=0;14 void add_edge(int u,int v){15 e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;16 return;17 }18 int f[mxn][mxn][2];//第三維0表示不返回根節點,1表示返回根節點 19 int w[mxn];20 int n,m;21 void dp(int u,int fa){22 int i,j,k;23 for(i=hd[u];i;i=e[i].nxt){24 int v=e[i].v;25 if(v==fa)continue;26 dp(v,u);27 for(j=m;j;--j){28 for(k=1;k<=j;++k){29 f[u][j][1]=max(f[u][j][1],f[u][j-k][1]+f[v][k-2][1]);30 f[u][j][0]=max(f[u][j][0],f[u][j-k][1]+f[v][k-1][0]);31 f[u][j][0]=max(f[u][j][0],f[u][j-k][0]+f[v][k-2][1]);32 }33 }34 }35 return;36 }37 int main(){38 while(scanf("%d%d",&n,&m)!=EOF){39 memset(e,0,sizeof e);40 memset(hd,0,sizeof hd);41 memset(f,0,sizeof 0);42 mct=0;43 int i,j;44 for(i=1;i<=n;i++){45 scanf("%d",&w[i]);46 for(j=0;j<=m;j++){47 f[i][j][0]=f[i][j][1]=w[i];48 }49 }50 int u,v;51 for(i=1;i<n;i++){52 scanf("%d%d",&u,&v);53 add_edge(u,v);54 add_edge(v,u);55 }56 dp(1,0);57 printf("%d\n",f[1][m][0]);58 }59 return 0;60 }
POJ2486 Apple Tree