hdu 1561The more, The Better(树形dp&01背包)

The more, The Better

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4949    Accepted Submission(s): 2918

Problem Description

ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物。但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡。你能帮ACboy算出要获得尽量多的宝物应该攻克哪M个城堡吗?

Input

每个测试实例首先包括2个整数,N,M.(1 <= M <= N <= 200);在接下来的N行里,每行包括2个整数,a,b. 在第 i 行,a 代表要攻克第 i 个城堡必须先攻克第 a 个城堡,如果 a = 0 则代表可以直接攻克第 i 个城堡。b 代表第 i 个城堡的宝物数量, b >= 0。当N = 0, M = 0输入结束。

Output

对于每个测试实例,输出一个整数,代表ACboy攻克M个城堡所获得的最多宝物的数量。

Sample Input

3 2
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0

Sample Output

5
13

Author

8600

Source

HDU 2006-12 Programming Contest

Recommend

LL   |   We have carefully selected several similar problems for you:  1011 2639 3033 2955 2415

中文题目,不解释。把他转换成一颗树,然后每次从父亲节点找到最优子树,然后往上传递即可。需要01背包,因为一个子树只能用一次。

dp方程为:

dp[cur][j]=max(dp[cur][j],dp[cur][k]+dp[p][j-k]);

其中cur是p的父亲节点。

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1561

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#define ll long long
using namespace std;
const int maxn=205;

ll dp[maxn][maxn];
int visi[maxn];
vector <int> mq[maxn];
int n,m;

void dfs(int cur)
{
    visi[cur]=1;
    for(int i=0;i<mq[cur].size();i++)
    {
        int p=mq[cur][i];
        if(!visi[p])
            dfs(p);

        for(int j=m+1;j>=1;j--)
            for(int k=1;k<j;k++)
            {
                if(dp[cur][k]!=-1&&dp[p][j-k]!=-1)
                dp[cur][j]=max(dp[cur][j],dp[cur][k]+dp[p][j-k]);
            }
    }
}

int main()
{
    int a,i,j;
    ll b;

    while(~scanf("%d%d",&n,&m)&&(m+n))
    {
        for(i=0;i<=n;i++)
            mq[i].clear();
        memset(visi,0,sizeof(visi));
        memset(dp,-1,sizeof(dp));
        /*for(i=0;i<=n;i++)
            for(j=1;j<=m+1;j++)
                dp[i][j]=-1;*/

        for(i=1;i<=n;i++)
        {
            scanf("%d%I64d",&a,&b);
            mq[a].push_back(i);
            dp[i][1]=b;
        }
        dp[0][1]=0;

        dfs(0);
        printf("%I64d\n",dp[0][m+1]);
    }

    return 0;
}

hdu 1561The more, The Better(树形dp&01背包),布布扣,bubuko.com

时间: 2024-08-25 08:16:34

hdu 1561The more, The Better(树形dp&01背包)的相关文章

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

poj 3345 Bribing FIPA 【树形dp + 01背包】

Bribing FIPA Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4274   Accepted: 1337 Description There is going to be a voting at FIPA (Fédération Internationale de Programmation Association) to determine the host of the next IPWC (Interna

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

hdu 1561 树形dp+0-1背包

1 /* 2 根据先后关系,可以建立一棵树 3 dp[i][j]表示第i个节点选j个的最大值 4 dp[i][j]=max(sigma(dp[c[i][ki]])) 5 sigma(dp[c[i][ki]])表示从i的儿子节点中一共选取j个点的最大值 6 */ 7 /*#include <iostream> 8 #include <cstdio> 9 #include <cstring> 10 #include <vector> 11 using names

poj 1947(树形DP+01背包)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10663   Accepted: 4891 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. Th

hihoCoder1055.刷油漆——树形Dp+01背包

http://hihocoder.com/problemset/problem/1055 一棵有根树,包含根节点1,选择M个连续的节点,使得权值最大 dp[u][j] 表示以i为根的子树中,选择包含根节点的j个连续节点所能获得的最大权值 枚举子节点选择的个数,儿子节点选择的个数,当做01背包 #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include

hihoCoder#1055 : 刷油漆 (树形DP+01背包)

题目大意:给一棵带点权的树,现在要从根节点开始选出m个连通的节点,使总权值最大. 题目分析:定义状态dp(u,m)表示在以u为根的子树从根节点开始选出m个点连通的最大总权值,则dp(u,m)=max(dp(u,m),dp(u,m-k)+dp(son,k)),其中0<=k<m.这是01背包,k应该从大往小枚举. 代码如下: # include<iostream> # include<cstdio> # include<cmath> # include<v

hdu 1561 The more, The Better(树形dp+01背包)

The more, The Better Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些城堡不能直接攻克,要攻克这些城堡必须先攻克其他某一个特定的城堡.你能帮ACboy算出要获得

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m