POJ 2468 Apple Tree 树上瞎搞分组背包

昨晚Debug了好久始终找不出哪里错了,今早再一看发现自己已荣升逗比Beta 2.0 Version.

个人感觉此题为HDU 4003 的弱化版。

把每棵子树都看成一类商品,在每类商品中至多选一件。则问题转化为最基本的分组背包问题。

dp[s][c][k] c == 1时,表示在s结点不返回时走K的最大收益,c == 0时,表示在s结点重新返回时走k步的最大收益。

可以dfs从底到顶更新dp。值得一提的是,要保证每个结点的dp[s][c][k]只会被dfs一次,即不能重复更新。

因为重复的dfs不能保证每类商品中之多选一件。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long LL
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 1000000007
#define LM(a,b) (((ULL)(a))<<(b))
#define RM(a,b) (((ULL)(a))>>(b))

using namespace std;

const LL MAXN = 10010;

struct N
{
    int u,v,w,next;
} edge[MAXN*2];

int head[MAXN];

int Top;

void Link(int u,int v,int w = -1)
{
    edge[Top].u = u;
    edge[Top].v = v;
    edge[Top].w = w;
    edge[Top].next = head[u];
    head[u] = Top++;
}

void Init_head_Top(int n)
{
    memset(head,-1,sizeof(int)*(n+2));
    Top = 0;
}

LL dp[110][2][210];

LL price[210];

void dfs(int s,int pre,int c,int k)
{
    if(k < 0)
        return ;

    if(dp[s][c][k] != -1)
        return ;

    dp[s][1][0] = price[s];
    dp[s][0][0] = price[s];

    for(int p = head[s] ; p != -1; p = edge[p].next)
    {
        if(edge[p].v != pre)
        {
            if(c == 1)
                dfs(edge[p].v,s,1,k-1);
            dfs(edge[p].v,s,0,k-2);
        }
    }

    for(int p = head[s] ; p != -1; p = edge[p].next)
    {
        if(edge[p].v != pre)
        {
            if(c == 1)
            {
                for(int i = k;i >= 0; --i)
                {
                    if(dp[s][1][i] != -1)
                    {
                        for(int j = k-i-2;j >= 0; --j)
                        if(dp[edge[p].v][0][j] != -1)
                            dp[s][1][i+j+2] = max(dp[s][1][i+j+2],dp[s][1][i] + dp[edge[p].v][0][j]);
                    }
                    if(dp[s][0][i] != -1)
                    {
                        for(int j = k-i-1;j >= 0 ; --j)
                        if(dp[edge[p].v][1][j] != -1)
                            dp[s][1][i+j+1] = max(dp[s][1][i+j+1],dp[s][0][i] + dp[edge[p].v][1][j]);
                    }
                }
            }

            for(int i = k;i >= 0; --i)
            {
                if(dp[s][0][i] != -1)
                {
                    for(int j = k-i-2;j >= 0; --j)
                    if(dp[edge[p].v][0][j] != -1)
                        dp[s][0][i+j+2] = max(dp[s][0][i+j+2],dp[s][0][i] + dp[edge[p].v][0][j]);
                }
            }

        }
    }

    if(c == 1)
    {
        for(int i = 0;i <= k; ++i)
            dp[s][c][i] = max(dp[s][c][i],(LL)0);
    }

    for(int i = 0;i <= k; ++i)
        dp[s][0][i] = max(dp[s][0][i],(LL)0);

}

int main()
{
    //freopen("ts.txt","w",stdout);
    int n,k,i,u,v;

    while(scanf("%d %d",&n,&k) != EOF)
    {
        memset(dp,-1,sizeof(dp));

        Init_head_Top(n);

        for(i = 1; i <= n; ++i)
            scanf("%lld",&price[i]);

        for(i = 1; i < n; ++i)
        {
            scanf("%d %d",&u,&v);
            Link(u,v);
            Link(v,u);
        }

        dfs(1,-1,1,k);

        LL Max = 0;
//        int ww = k;
//        for(i = 1;i <= n; ++i)
//        {
//            for(int j = 0;j <= 1; ++j)
//            {
//                for(k = 0;k <= ww; ++k)
//                {
//                    if(dp[i][j][k] > 0)
//                    printf("i = %d j = %d k = %d dp = %lld\n",i,j,k,dp[i][j][k]);
//                }
//            }
//        }
//
//        k = ww;

        for(i = 0; i <= k; ++i)
        {
            Max = max(Max,dp[1][1][i]);
        }

        printf("%lld\n",Max);
    }

    return 0;
}

POJ 2468 Apple Tree 树上瞎搞分组背包

时间: 2024-10-11 18:06:37

POJ 2468 Apple Tree 树上瞎搞分组背包的相关文章

HDU 4003 Find Metal Minaral 树上瞎搞分组背包

对于分组背包,每组选且只选一件商品的写法只想出了二维数组的写法. dp[s][k] 表示 在前s类中选取价格为 k 的商品的最优解. dp[s][k] = max( dp[s-1][k-product[s][j].c] + product[s][j].w).dp[s][k]每次只会有dp[s-1]更新得到,保证了前s-1类商品的选取. 对于此题,可以把每棵子树的情况看成一类商品,递归求解. dp[s][k]表示在s子树上投入k个机器人时的最优解. 当k == 0时,表示在s点投入1个机器人,且此

poj 3321:Apple Tree(树状数组,提高题)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 5629 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

POJ 3321 Apple Tree (dfs+线段树)

题目大意: 修改树上的节点,然后求子树的和. 思路分析: dfs 重新编号,烂大街了... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 100005 #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e using namespace std

POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)

POJ - 3321 Apple Tree Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very muc

【树状数组】POJ 3321 Apple Tree

/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i

POJ 2486 Apple Tree 树形DP+分组背包

链接:http://poj.org/problem?id=2486 题意:一棵(苹果)树,树上有N个结点(N<=100),起点是结点1.每个结点上有若干个苹果,我可以进行K步操作(K<=200),每次操作是从当前结点移动到相邻的结点,并且到了相邻的结点以后会吃掉上面的所有苹果并且苹果不再长出来,相邻是指两个结点之间有边相连.问在K步操作之后最多可以吃掉多少个苹果. 思路:刚入手的时候觉得是一般的树形背包问题,dp[i][j]代表的是以i为根的子树中走j个结点所能吃到的苹果数,来进行状态转移,但

POJ 3321 Apple Tree (树状数组)

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21191   Accepted: 6436 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

#5 DIV2 A POJ 3321 Apple Tree 摘苹果 构建线段树

Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25232   Accepted: 7503 Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been

POJ 3321 Apple Tree

题目链接:http://poj.org/problem?id=3321 解题思路:dfs加时间戳然后简单树状数组单点更新区间查询即可. 代码: 1 const int maxn = 1e5 + 5; 2 struct Edge{ 3 int to, next; 4 }; 5 Edge edges[maxn]; 6 int head[maxn], tot; 7 int st[maxn], ed[maxn], cnt; 8 int bit[maxn], status[maxn], n, m; 9 1