ACM: 动态规划题 poj 2486

来源:互联网 发布:金山tw域名遭抢注 编辑:程序博客网 时间:2024/06/07 23:00
Apple Tree
Description
Wshxzt is a lovely girl. Shelikes apple very much. One day HX takes her to an apple tree. Thereare N nodes in the tree. Each node has an amount of apples. Wshxztstarts her happy trip at one node. She can eat up all the apples inthe nodes she reaches. HX is a kind guy. He knows that eating toomany can make the lovely girl become fat. So he doesn’t allowWshxzt to go more than K steps in the tree. It costs one step whenshe goes from one node to another adjacent node. Wshxzt likes applevery much. So she wants to eat as many as she can. Can you tell howmany apples she can eat in at most K steps.

Input

There are several test cases inthe input
Each test case contains three parts.
The first part is two numbers N K, whose meanings we have talkedabout just now. We denote the nodes by 1 2 ... N. Since it is atree, 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 nonnegativeand not bigger than 1000). The ith number is the amount of applesin Node i.
The third part contains N-1 line. There are two numbers A,B in eachline, 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 themaximal 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

题意: 女孩Wshxzt喜欢吃苹果, HX带她到一棵苹果树中, 苹果树每个节点有一定数量的苹果,

     HX不想女孩变胖, 限制她可以在苹果树上走K步, 相邻节点之间长度为1步, 现在要你

     帮女孩用K步计算出可以吃到最大苹果数量.

 

解题思路:

     1. 思路类似于poj 1947, 用2棵子树进行合并操作来递推问题的解. 但是本题有一个

        难点就是怎么讨论是否回到树的根.

      2.因此, 设状态dp[i][j][0/1]: 表示以i为子树根, 走j步回到(0)根或则不回到(1)

        根, 可以获得苹果的最大数量.

        动态方程:

        (1) dp[i][j][0] = max(dp[i][j][0],dp[i][j-k][0]+dp[v][k-2][0]);

        回到根, 可以分成2棵子树的问题, 2棵子树都回到自己的子树根, k-2是因为

        需要2步来连接这两棵子树, 因此其中一棵子树分配的步数需要减少2.

        (2). dp[i][j][1]需要分两种情况讨论.

         =max(dp[i][j][1], dp[i][j-k][1]+dp[v][k-2][0],dp[i][j-k][0]+dp[v][k-1][1]);

         情况一:先走dp[v][k-2][0]情况, 同(1)一样分配k-2步, 回到子树根v, 再往另外一边

                 寻找苹果.因此另一边不需要回到子树根i.

         情况二:先走dp[i][j-k][0]回到子树根i, 再往dp[v][k-1][1]一边行走k-1步(因为连接

                两棵子树的的边只走一次)寻找苹果不需要回到子树根v.

      3. 方程出来, 接着还是自底向上递推结果即可.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;
#define MAX 105

int n, K;
int g[MAX][MAX], num[MAX], a[MAX];
int dp[MAX][MAX*2][2];
bool vis[MAX];

inline int max(int a, int b)
{
 return a > b ? a : b;
}

void dfs(int u)
{
 vis[u] = true;
 int i, j, k, v;
 for(i = 0; i <= K; ++i)
  dp[u][i][0] = dp[u][i][1] =a[u];
 for(v = 0; v < num[u]; ++v)
 {
  if(vis[ g[u][v] ])continue;
  dfs(g[u][v]);
  for(j = K; j >=0; --j)
  {
   for(k = 1;j-k >= 0; ++k)
   {
    dp[u][j][1]= max(dp[u][j][1], dp[u][j-k][0]+dp[ g[u][v] ][k-1][1]);
    if(k% 2 == 0)
    {
     dp[u][j][1]= max(dp[u][j][1], dp[u][j-k][1]+dp[ g[u][v] ][k-2][0]);
     dp[u][j][0]= max(dp[u][j][0], dp[u][j-k][0]+dp[ g[u][v] ][k-2][0]);
    }
   }
  }
 }
}

int main()
{
 int i, u, v;
// freopen("input.txt", "r", stdin);
 while(scanf("%d %d",&n,&K) != EOF)
 {
  memset(num, 0,sizeof(num));
  memset(g, 0, sizeof(g));
  memset(dp, 0,sizeof(dp));
  memset(vis, false,sizeof(vis));
  for(i = 1; i <=n; ++i)
   scanf("%d",&a[i]);
  for(i = 1; i <n; ++i)
  {
   scanf("%d%d",&u, &v);
   g[u][num[u]++]= v;
   g[v][num[v]++]= u;
  }

  dfs(1);
  int result = max(dp[1][K][0],dp[1][K][1]);
  printf("%d\n", result);
 }
 return 0;
}

0 0
原创粉丝点击