题目:这里

题意:

感觉并不能表达清楚题意,所以

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
 
首先,这个最终答案是与点的度有关,由于是个树,可以知道最后所有点的度数和是n*2-2,还有,每个点至少得有一个度,所以最终答案得先加上f[1]*n,然后现在
还剩下n-2个度,需要在n个点里分配,使得分配之后的权值最大,但是这个分配由于是有关联的,一个点的度数加了1之后必须得有另一个点的度数也加1,所以我们的
分配方案还得满足这个条件,不能随意分配,但是通过随意取几个n值构造一下树发现,n-2个度任意分给n个点的方案能够满足构造出一棵树,而且这个构造还挺有
规律,有递推性,所以大胆认为可以任意分配,好,现在n-2个度分配给n个点,每次可以分配1到n-1个度,问怎么分配值f()最大,这不就是一个背包么,还是一个完全
背包。再注意一下这是在每个点已经有了一个度的前提下,所以得减去f[1]。
 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; #define inf 0x3f3f3f3f
const int M = 1e4 + ;
int dp[M],a[M]; int max(int x,int y){return x>y?x:y;} int main()
{
int t,n;
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for (int i= ; i<n ; i++) {
scanf("%d",&a[i]);
if (i!=) a[i]-=a[];
}
//int pa=n*2-2;
for (int i= ; i<=n ; i++) dp[i]=-inf;
dp[]=;//dp[1]=a[1];
for (int i= ; i<n ; i++) {
for (int j= ; j+i-<=n- ; j++)
dp[i+j-] = max(dp[i+j-],dp[j]+a[i]);
}
printf("%d\n",dp[n-]+n*a[]);
}
return ;
}
 

hdu 5534 (完全背包) Partial Tree的更多相关文章

  1. HDU 5534 完全背包

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

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

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

  4. HDU 5534 Partial Tree 完全背包

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

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

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

  6. HDU 5534/ 2015长春区域H.Partial Tree DP

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

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

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

  8. Partial Tree

    Partial Tree 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 完全背包 做这题前去学习了下完全背包,觉得这个优化简直神技!(以前都是 ...

  9. Partial Tree(DP)

    Partial Tree http://acm.hdu.edu.cn/showproblem.php?pid=5534 Time Limit: / MS (Java/Others) Memory Li ...

随机推荐

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. NK3C系统中ID的汉语名称

    系统中的ID有需要显示给使用者看的,统一用"编号",不要用ID或者编码 例如: SampleId 样本编号 正确 样本编码 错误 样本ID 错误 如果见到系统中有用到的,修正即可. ...

  3. 7、SQL Server索引、表压缩

    索引 什么是索引? 索引是一种磁盘上的数据结构,建立在表或视图的基础上.使用索引可以使数据的获取更快更高校,也会影响其他的一些性能,如插入或更新等. 索引主要分为两种类型:聚集索引和非聚集索引. 字典 ...

  4. 安装zabbix报错configure: error: libcurl library not found

    libcurl 和libcurl-devel都已经安装 且大于7.13.1 但还是报错,这时需要在configure 指定curl路径即可 ./configure --prefix=/usr/loca ...

  5. C语言字符输出格式化

    转自http://blog.csdn.net/pbymw8iwm/article/details/8153226 符号属性 长度属性 基本型 所占 位数 取值范围 输入符举例 输出符举例 -- -- ...

  6. php curl get

    function curlGet($url){ $ch = curl_init(); $header = "Accept-Charset: utf-8"; curl_setopt( ...

  7. ASP.NET Misconfiguration: Excessive Session Timeout

    Abstract: An overly long authentication timeout gives attackers more time to potentially compromise ...

  8. {ICIP2014}{收录论文列表}

    This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinc ...

  9. C++中extern关键字用法小结

    总结C++中关于extern关键字的用法. 1.变量的生明和定义中 C++语言支持分离式编译机制,该机制允许将程序分割为若干个文件,每个文件可被独立编译.为了将程序分为许多文件,则需要在文件中共享代码 ...

  10. 服务器控件中使用<%#...>, JS和html控件中使用<%=...>

    //在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...