POJ 2486 树形背包

题目

Apple Tree
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9094 Accepted: 3022
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

题意

一棵树,从根出发走一定的步数,问最多能得到的权值是多少。

题解

如果不能往回走我们可以设dp[i][j]为在第i个节点走j步能得到的最大结果。状态转移为:
dp[root][j] = max(dp[son][j-1])(son为根的所有儿子节点)
但是如果考虑可以返回就不能
这么做了。
定义dp[p][i][j],p为0表示在i中走j步最终回到i,p为1表示在i中走j步最终不回到i。
由此可知
从root到v然后又从v到root增加两条边

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

增加一条从root到v的边

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

得返回,加两条边

dp[1][root][j + 2] = max(dp[1][root][j + 2], dp[0][v][k] + dp[1][root][j - k]);
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
#include <iomanip>
#define m0(a) memset(a,0,sizeof(a))
#define mm(a) memset(a,0x3f,sizeof(a))
#define m_1(a) memset(a,-1,sizeof(a))
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
#define lowbit(a) ((a)&(-a))
#define FFR freopen("data.in","r",stdin)
#define FFW freopen("data.out","w",stdout)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
const ld PI = acos(-1.0);

using namespace std;
#define SIZE (150)

int G[SIZE][SIZE];
int cnt[SIZE];
int apple[SIZE];
int n, K;
int dp[2][SIZE][SIZE*2];
int vis[SIZE];

void dfs(int root) {
    int i, j, k;
    f(i, 0, K) dp[0][root][i] = dp[1][root][i] = apple[root];
    vis[root] = 1;
    f(i, 1, cnt[root]) {
        int v = G[root][i];
        if (vis[v] == 0) {
            dfs(v);
            fi(j, K, 0) {
                f(k, 0, j) {
                    dp[0][root][j + 2] = max(dp[0][root][j + 2], dp[0][v][k] + dp[0][root][j - k]);
                    dp[1][root][j + 1] = max(dp[1][root][j + 1], dp[1][v][k] + dp[0][root][j - k]);
                    dp[1][root][j + 2] = max(dp[1][root][j + 2], dp[0][v][k] + dp[1][root][j - k]);
                }
            }
        }
    }
}

int main() {
    //ios_base::sync_with_stdio(false); cin.tie(0);
    while (~scanf("%d%d",&n,&K)) {
        int i;
        m0(cnt);
        f(i, 1, n)scanf("%d", &apple[i]);
        f(i, 1, n - 1) {
            int x, y;
            scanf("%d%d", &x, &y);
            G[x][++cnt[x]] = y;
            G[y][++cnt[y]] = x;
        }

        m0(dp);
        m0(vis);
        f(i, 1, n) dp[0][i][0] = dp[1][i][0] = apple[i];
        dfs(1);
        printf("%d\n", dp[1][1][K]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值