hdu 5534 Partial Tree【完全背包】

Partial Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 864    Accepted Submission(s): 437

Problem Description

In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
You find a partial tree on the way home. This tree has n nodes but lacks of n−1 edges. You want to complete this tree by adding n−1 edges. There must be exactly one path between any two nodes after adding. As you know, there are nn−2 ways to complete this tree, and you want to make the completed tree as cool as possible. The coolness of a tree is the sum of coolness of its nodes. The coolness of a node is f(d), where f is a predefined function and d is the degree of this node. What's the maximum coolness of the completed tree?

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 n−1 integers f(1),f(2),…,f(n−1).

1≤T≤2015
2≤n≤2015
0≤f(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

Source

2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)


题目大意:

给你n个节点,对于每个节点都引入一个度的概念,对于每一种度数的点,对应都有一个价值:f(di(di表示点的度数))=X对应的价值,比如f(1)=2:表示度为1的这种节点,价值为2.然后给你n-1条边,让你使得这n个节点形成一颗树,问这棵树的最大价值和。


思路:


1、首先,对于一颗树来讲,有n个节点,就一定有2*n-2的度数和,对于问题的转化:有2*n-2容量的一个背包,有价值为X的体积为di的物品,每个物品可以拿多次,但是所有物品放在一起只能而且必须拿n个,而且必须凑成体积为2*n-2,问在这种情况下,最大能拿多少价值的物品。


2、这样,问题就转化到了一个二维限制背包上来,然后代码实现需要:O(n*n*(n*2-2)*T)的时间复杂度,显然会TLE,需要优化算法............(接下来的思路来源自各位巨巨的题解)


3、如果能够将一个二维限制背包问题从3层for优化下去,我们只能从新考虑这个问题,在代码上优化算法有点不太可能。


4、那么考虑这样一个问题:一颗树,一个点的最小度数是多少:显然是1.那么思路如果确定到这条路上来,那么就距离Ac不远了:将每个点,我都分配一个度数。然后问题就转化到了:n-2个度数,分配到n个点上。那么这个时候,我们就将二维限制背包问题,转化成一个完全背包问题上来:n-2的容积,n-2个物品,每个物品的体积为i-1,价值为

f【i】-a【1】。


5、思维确立到了这里,整个思路也就确立完成了,剩下的就是跑一遍完全背包了;


Ac代码:


#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int a[25000];
int dp[25000];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        int ans=a[1]*n;
        int v=n-2;
        for(int i=2;i<n;i++)
        {
            a[i]-=a[1];
        }
        memset(dp,-0x3f3f3f3f,sizeof(dp));
        dp[0]=0;
        for(int i=2;i<n;i++)
        {
            for(int j=i-1;j<=v;j++)
            {
                dp[j]=max(dp[j],dp[j-i+1]+a[i]);
            }
        }
        printf("%d\n",ans+dp[v]);
    }
}






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值