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. Mybatis之Mapper动态代理

    一.什么是Mapper的动态代理 采用Mapper动态代理方法只需要编写相应的Mapper接口(相当于Dao接口),那么Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同Dao接 ...

  2. 九度OJ 1028:继续畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...

  3. c++动态绑定的技术实现

    1 什么是动态绑定 有一个基类,两个派生类,基类有一个virtual函数,两个派生类都覆盖了这个虚函数.现在有一个基类的指针或者引用,当该基类指针或者引用指向不同的派生类对象时,调用该虚函数,那么最终 ...

  4. 【Android】Android中AlertDialog对话框的使用实例

    package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...

  5. LeetCode 017 4Sum

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  6. gradle 跳过junitTest的方法

    Web项目中不长会写JunitTest,但也会写.gradle build的时候回执行test 这项task.如果想跳过,通常有几种方法: 1.在build.gradle 文件中禁用task test ...

  7. Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. LeetCode:递增的三元子序列【334】

    LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i ...

  9. JS 中的面向对象

    创建对象的集中常见方式 1 . 使用 Object 或 对象字面量创建对象 2 . 工厂模式创建对象 3 . 构造函数模式创建对象 4 . 原型模式创建对象 1 . 使用 Object 或 对象字面量 ...

  10. web 全栈 学习 2 一个好的页面是如何炼成的

    第一章:Web页面内容的构成2.Web内容的分工一个Web页面可能的构成(视觉上看):①文字.链接.标题②交互入口(表单元素)③图片(哪些类型)④动画 Flash动画 HTML5 CSS3 动画⑤音视 ...