hdu 5534(dp)

Input
The first line contains an integer  T  indicating the total number of test cases.
Each test case starts with an integer  n  in one line,
then one line with  n1  integers  f(1),f(2),,f(n1) .

1T2015
2n2015
0f(i)10000
There are at most  10  test cases with  n>100 .
 

Output
For each test case, please output the maximum coolness of the completed tree in one line.
 

Sample Input
  
  
2 3 2 1 4 5 1 4
 

Sample Output
  
  
5 19

题意:老实说,开始看了半天并没有看懂题- -。

给你n个点,然后要求添加n-1条边,每个节点都有度(入度+出度),度的数量对应不同的权值,求树的最大权值


思路:总共有2*(n-1)个度,首先,每个点都先有一个度,然后分配剩下的n-2个度给n个点使它们最大,

于是就成了背包问题                                                      /*  感觉动规怎么都不会,是时候去学学了         

 /*表示从题没看懂那一刻,就注定做不出来,于是只有去看别人的报告了- -


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

//看懂题好不容易- -

using namespace std;

int a[20005];
int dp[20005];

int main()
{
    int cas,n;
    scanf("%d",&cas);
    while(cas -- )
    {
        scanf("%d",&n);
        for(int i = 0; i < n-1; i++)
            scanf("%d",a+i);
        int all = n-2;
        int ans = a[0]*n;

        for(int i = 1; i <= n; i++)
            dp[i] = -0x3f3f3f3f;
        dp[0]= ans;
        for(int i = 1; i < n-1; i++)
            a[i] -= a[0];

        for(int i = 1; i <= all; i++)
        {
            for(int j = i; j <= all; j++)
            {
                dp[j] = max(dp[j],dp[j-i] + a[i]);
            }
        }
        printf("%d\n",dp[all]);
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值