Partial Tree

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5534

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

HINT

题意

给你n个点,让你构造出一棵树

假设这棵树最后度数为k的点有num[k]个,那么这棵树的价值为sigma(num[i]*f[i])

其中f[i]是已经给定的

题解:

dp,我们首先给所有点都分配一个度数,那么还剩下n-2个度数没有分配

我们就可以dp了

dp[i]表示当前分配了i点度数时获得的最优值是多少,那么直接暴力转移就好了

背包DP

代码

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int n;
int dp[];
int f[];
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n-;i++)
scanf("%d",&f[i]);
for(int i=;i<=n;i++)
dp[i]=-;
dp[]=n*f[];
for(int i=;i<n-;i++)
f[i]-=f[];
n-=;
for(int i=;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
dp[j]=max(dp[j],dp[j-i]+f[i]);
}
}
printf("%d\n",dp[n]);
}
}

hdu 5534 Partial Tree 背包DP的更多相关文章

  1. HDU 5534 Partial Tree 完全背包

    一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...

  2. HDU - 5534 Partial Tree(每种都装的完全背包)

    Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...

  3. 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree

    Partial Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  4. HDU 5534 Partial Tree (完全背包变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...

  5. hdu 5534 Partial Tree(完全背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...

  6. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  7. 2015长春 HDU 5534 Partial Tree

    题意:有n个结点,n-1条边,现在要把这n个结点连成一棵树,给定了f(i),表示度为i的结点的价值是f(i).现在问如何连能够使得Σf(i)的值最大. 思路:每个点至少一个度,所以可分配的度数为n-2 ...

  8. HDU 5534 Partial Tree

    2015 ACM/ICPC 长春现场赛 H题 完全背包 #include<cstdio> #include<cstring> #include<cmath> #in ...

  9. hdu 5234 Happy birthday 背包 dp

    Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

随机推荐

  1. 【转】话说我打算一天学完object c语法,系列1--------来自书Objective-c程序设计

    原文网址:http://blog.csdn.net/zengraoli/article/details/8993466 类型: NSString NSInteger NSLong控制台输出 NSObj ...

  2. [转]Android调用so文件(C代码库)方法详解

    一.为什么调用c的dll要用源码编译成so库 Android系统是基于linux内核的移动终端系统,而dll是在windows环境下生成和调用的c库,所以不可以直接为android系统调用. 二.安装 ...

  3. 怎样查看Eclipse是32位还是64位

    首先进入到Eclipse的安装目录,如下图:   查找到文件名为"eclipse.ini" 文件,使用文本编辑工具,或记事本打开,如下图: 如图中的红框所示,如果是win32.x8 ...

  4. QT-【转】Qt 4迁移至Qt 5

    将Qt 4代码迁移到Qt 5还是比较简单的.实际上,在Qt 5开发过程中就已经注意了与Qt 4代码保持兼容性. 与Qt 3到Qt 4的迁移不同,Qt 5的核心类库并没有做大的API的修改,只有几个新的 ...

  5. 我用dedecms有感

    ---恢复内容开始--- 最近接了一个私单,简单的学校网站,注意,我一看上去是感觉很快,仿站,对方说这个东西你三天就能搞定啦,我也这么想的 (没经验啊) 接下来,我想都没想就用dedecms去做,之前 ...

  6. 【Spark学习】Apache Spark配置

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137969.html Spar ...

  7. libvirt虚拟系统如何增加usb设备

    之前干这些事情都是通过virt-manager来搞定的.不过由于这个图形界面不太方便,而且现在没法打开(具体原因不详,每次打开提示一些方法未实现什么的),所以试下用libvirt的命令virsh来搞定 ...

  8. Apache Spark Streaming的简介

    Spark Streaming通过将流数据按指定时间片累积为RDD,然后将每个RDD进行批处理,进而实现大规模的流数据处理.其吞吐量能够超越现有主流流处理框架Storm,并提供丰富的API用于流数据计 ...

  9. (转)关于CoreData的一个工具Mogenerator的使用

    最近看到用CoreData时使用的工具Mogenerator,发现网上介绍其具体使用的不多,特此简单整理一下,关于CoreData这里就不具体说了,使用就用MagicalRecord,用起来真是太方便 ...

  10. HDU 5802 Windows 10 (贪心+dfs)

    Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...