Hrbust 1302 Wealthy Family【树型dp】

本文介绍了一种树形背包问题的解决方法,通过定义状态dp[i][j]表示到点i,拿了j个点的最大价值,并给出了状态转移方程及优化措施。文章还提供了完整的AC代码实现。

Wealthy Family
Time Limit: 10000 MSMemory Limit: 32768 K
Total Submit: 17(8 users)Total Accepted: 10(6 users)Rating: Special Judge: No
Description
While studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting caused by inheritance. One way to estimate the wealth of a family is to sum up the net worth of a set of k people such that no one in the set is an ancestor of another in the set. The wealth of the family is the maximum sum achievable over all such sets of k people. Since historical records contain only the net worth of male family members, the family tree is a simple tree in which every male has exactly one father and a non-negative number of sons. You may assume that there is one person who is an ancestor of all other family members.
Input
The input consists of a number of cases. Each case starts with a line containing two integers separated by a space: N (1 <= N <= 150,000), the number of people in the family, and k (1 <= k <= 300), the size of the set. The next N lines contain two non-negative integers separated by a space: the parent number and the net worth of person i (1 <= i <= N). Each person is identified by a number between 1 and N, inclusive. There is exactly one person who has no parent in the historical records, and this will be indicated with a parent number of 0. The net worths are given in millions and each family member has a net worth of at least 1 million and at most 1 billion.
Output
For each case, print the maximum sum (in millions) achievable over all sets of k people satisfying the constraints given above. If it is impossible to choose a set of k people without violating the constraints, print 'impossible' instead.
Sample Input
5 3
0 10
1 15
1 25
1 35
4 45
3 3
0 10
1 10
2 10
Sample Output
85
impossible
Source
The 2011 Rocky Mountain Regional Contest

题目大意:

一共给你N个点,最多只能拿K个点。

需要保证所拿的各个点的祖先都不在这K个点中。


思路(树型dp是窝第噩梦啊):


首先分析出这是一个很裸的树背包。

那么设定dp【i】【j】表示到点i,拿了j个点的最大价值(当然满足祖先的约数条件情况下)。

那么不难推出其状态转移方程:
dp【i】【j+k】=max(dp【i】【j+k】,dp【i】【j】+dp【v】【k】);

但是因为点的个数比较多,显然二维数组的内存是开不下的。

那么考虑在Dfs过程中设定为一维即可。


时间判定比较严格,需要对子树中点的个数进行统计,直接暴力背包会TLE.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
vector<int >mp[150500];
int val[150500];
int dp[350];
int n,m;
int Dfs(int u)
{
    int cur[350],tot=0;
    for(int i=0;i<=m;i++)dp[i]=cur[i]=-0x3f3f3f3f;
    cur[0]=dp[0]=0;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        int cnt=Dfs(v);
        for(int j=tot;j>=0;j--)
        {
            for(int k=1;k<=cnt&&(k+j)<=m;k++)
            {
                cur[k+j]=max(cur[k+j],dp[k]+cur[j]);
            }
        }
        tot=min(m,tot+cnt);
    }
    cur[1]=max(cur[1],val[u]);
    for(int i=0;i<=m;i++)
    {
        dp[i]=cur[i];
    }
    if(mp[u].size()==0)tot=1;
    return tot;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d%d",&x,&val[i]);
            mp[x].push_back(i);
        }
        Dfs(0);
        if(dp[m]<0)printf("impossible\n");
        else printf("%d\n",dp[m]);
    }
}




先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值