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
",dp[all]);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/Przz/p/5409686.html