ACM: 动态规划题 poj 2486

Apple Tree
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

题意: 女孩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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值