Partial Tree

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 nn nodes but lacks of n−1n−1 edges. You want to complete this tree by adding n−1n−1edges. There must be exactly one path between any two nodes after adding. As you know, there are nn−2nn−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)f(d), where ff is a predefined function and dd is the degree of this node. What's the maximum coolness of the completed tree?

InputThe first line contains an integer TT indicating the total number of test cases. 
Each test case starts with an integer nn in one line, 
then one line with n−1n−1 integers f(1),f(2),…,f(n−1)f(1),f(2),…,f(n−1).

1≤T≤20151≤T≤2015 
2≤n≤20152≤n≤2015 
0≤f(i)≤100000≤f(i)≤10000 
There are at most 1010 test cases with n>100n>100.OutputFor 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 因为每种装的范围为【1,+无穷】,然而分组背包的三次方会T,所以要想办法将其转化为完全背包模型。
因为每个点都至少有一度,所以我们预先放入n个一度,本来要放的2×n-2度现在只剩n-2度。
每当再次放入一个x度时,他的贡献为x-1度,在放入的同时减掉之前预先放好的一度。这样便保证了每个点至少有一度。
#include<bits/stdc++.h>
#define MAX 2018
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll; int a[MAX];
ll dp[MAX]; int main()
{
int t,n,m,i,j,k;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d",&a[i]);
}
memset(dp,-INF,sizeof(dp));
dp[]=;
for(i=;i<n;i++){
for(j=i-;j<=n-;j++){
dp[j]=max(dp[j],dp[j-(i-)]+a[i]-a[]);
}
}
printf("%I64d\n",dp[n-]+n*a[]);
}
return ;
}
 

HDU - 5534 Partial Tree(每种都装的完全背包)的更多相关文章

  1. HDU 5534 Partial Tree 完全背包

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

  2. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  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. ACM学习历程—HDU 5534 Partial Tree(动态规划)

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

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

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

  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. H - Partial Tree HDU - 5534 (背包)

    题目链接: H - Partial Tree  HDU - 5534 题目大意:首先是T组测试样例,然后n个点,然后给你度数分别为(1~n-1)对应的不同的权值,然后问你在这些点形成树的前提下的所能形 ...

随机推荐

  1. 性能测试--siege

    siege 这是Linux系统下的一个测试工具,完全使用C语言实现,可以对HTTP和FTP服务器进行负载和性能测试.通过使用Siege 提供的功能,可以很容易的制定测试计划:包括规定使用并发用户数.重 ...

  2. 如何在ubuntun中安装pycharm并将图标显示在桌面上

    安装pycharm首先要安装jdk. 可以通过java -V来查看是否安装了jdk.安装jdk的方法如下: 1 首先在oracle网站下载jdk,现在jdk是1.8的. 2 新建一个/usr/lib/ ...

  3. (转)JavaScript中==和===的区别

    ==   用于比较   判断 两者相等      ==在比较的时候可以转自动换数据类型 ===用于严格比较   判断两者严格相等     ===严格比较,不会进行自动转换,要求进行比较的操作数必须类型 ...

  4. 如何查看apache配置文件路径

    我是用https://lamp.sh/安装的,apache配置文件位置在:/usr/local/apache/conf/httpd.conf如果是直接安装的apache,配置文件应该在:/etc/ht ...

  5. join()方法作用

    当在主线程当中执行到t1.join()方法时,就认为主线程应该把执行权让给t1 废话不多说看代码: package com.toov5.thread; public class JoinThreadT ...

  6. Git 使用初步

    官网:https://git-scm.com/ 官方文档:https://git-scm.com/doc 比较简略的资料(对基本概念没有解释很清楚):http://wenku.baidu.com/li ...

  7. 虚拟化技术及ip netns简介

    虚拟化技术: Iass:infrastructure as a server 直接启动一个虚拟机,需要什么程序自己安装 Paas:platform as a servicce 启动一个虚拟机,并安装了 ...

  8. android程序的真正入口

    代码出自MtAndroid 3.1.2完全开发手册,适用于Android平台. 概述 android程序的真正入口是Application类的onCreate方法.它的继承关系如下所示: java.l ...

  9. h5打电话发短信写邮件怎么实现

    // 一.打电话<a href="tel:0755-10086">打电话给:0755-10086</a> // 二.发短信,winphone系统无效< ...

  10. kvm初体验之六:克隆

    目标:克隆vm1到vm1-clone 1. virsh suspend vm1 2. virt-clone --connect qemu:///system --original vm1 --name ...