DP:树DP

DP:树DP

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

The more, The Better

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5414    Accepted Submission(s): 3217




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

dp[i][j]表示以i为根节点j个子节点的最大值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<vector>
typedef long long LL;
using namespace std;
const int maxn=220;
int v[maxn];
int n,m;
int dp[maxn][maxn];
vector<int>s[maxn];
void tree_dp(int n,int f)
{
    int len=s[n].size();
    dp[n][1]=v[n];
    for(int i=0;i<len;i++)
    {
        if(f>1)  tree_dp(s[n][i],f-1);
        for(int j=f;j>=1;j--)
        {
            for(int k=1;k<=j;k++)
                dp[n][j+1]=max(dp[n][j+1],dp[n][j+1-k]+dp[s[n][i]][k]);
        }
    }
}
int main()
{
    int f;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        v[0]=0;
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=n;i++)
            s[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&f,&v[i]);
            s[f].push_back(i);
        }
        tree_dp(0,m+1);
        printf("%d\n",dp[0][m+1]);
    }
    return 0;
}

Anniversary party

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4329   Accepted: 2463

Description

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.

Input

Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go N – 1 lines that describe a supervisor relation tree. Each line of the tree specification has the form: 

L K 

It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line 

0 0 

Output

Output should contain the maximal sum of guests' ratings.

Sample Input

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

Sample Output

5

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
const int maxn=6005;
int dp[maxn][2],pre[maxn];
int visit[maxn],n;
void tree_dp(int x)
{
    visit[x]=1;
    for(int i=1;i<=n;i++)
    {
//        cout<<"111  "<<i<<endl;
        if(!visit[i]&&pre[i]==x)
        {
            tree_dp(i);
            dp[x][1]+=dp[i][0];
            dp[x][0]+=max(dp[i][1],dp[i][0]);
        }
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
        memset(visit,0,sizeof(visit));
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++)
           scanf("%d",&dp[i][1]);
        int x,y,root;
        while(~scanf("%d%d",&x,&y)&&(x+y))
        {
            pre[x]=y;
            root=y;
        }
        while(pre[root])
            root=pre[root];
 //       cout<<"fuck   "<<root<<endl;
        tree_dp(root);
        printf("%d\n",max(dp[root][0],dp[root][1]));
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117734.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • minipcie usb总线_ipadmini2换wifi模块

    minipcie usb总线_ipadmini2换wifi模块1、概述EC20R2.1MiniPCIe-C模块是PCIExpressMiniCard1.2标准接口LTE模块。本文章主要讲解了如何驱动EC20R2.1MiniPCIe-C模块的硬件电路设计,主要包含有:电源设计通讯接口SIM卡的防护1.1、EC20R2.1MiniPCIe-C模块引脚分配1.2、EC20R2.1MiniPCIe-C模块引脚描述引脚号miniPCIE引脚名模块引脚名I/O功能描述备注1WAKE

    2022年9月7日
    0
  • 2016年总结-JAVA程序员

    2016年总结-JAVA程序员

    2020年11月12日
    177
  • 扫描主机漏洞的工具_漏洞扫描工具有哪些

    扫描主机漏洞的工具_漏洞扫描工具有哪些0x00说明:这是一款基于主机的漏洞扫描工具,采用多线程确保可以快速的请求数据,采用线程锁可以在向sqlite数据库中写入数据避免databaseislocked的错误,采用md5哈希算法确保数据不重复插入。本工具查找是否有公开exp的网站为shodan,该网站限制网络发包的速度,因而采用了单线程的方式,且耗时较长。功能:查找主机上具有的CVE查找具有公开EXP的CVE0x…

    2022年9月12日
    0
  • 千万别让这些举动断送了你的职业前程-好文共分享[通俗易懂]

    千万别让这些举动断送了你的职业前程-好文共分享

    2022年2月7日
    43
  • 压缩包文件的密码如何破解[通俗易懂]

    压缩包文件的密码如何破解[通俗易懂]压缩包文件的打开密码不知道或者忘记了,导致不能解压压缩包文件,那么想要破解或者想要找回压缩包打开密码需要破解软件的帮助了,比如:奥凯丰压缩包解密大师破解rar、zip、7z格式的压缩包的打开密码,把文件添加到软件中,选择一个找回方法就可以开始破解找回密码了…

    2022年4月30日
    57
  • 什么是C语言数组地址

    什么是C语言数组地址还记得以前有和同事聊过C语言数组这个概念,那时候大家都还不是掌握的很好,总会搞错数组的地址。但是总有人会对数组的地址这个概念产生怨念,他们认为一个数组a本身就是地址,殊不知数组名a只是其首元素的地址,而&a才是数组a的地址。拓展:假设有一个数据inta[5];那么,a代表的是a[0]的地址,换句话说,a等价于&a[0],假如这个地址值是0x123,那么a+1的值是0…

    2022年7月22日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号