hdu 5534 Partial Tree 背包DP
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的更多相关文章
- HDU 5534 Partial Tree 完全背包
一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...
- HDU - 5534 Partial Tree(每种都装的完全背包)
Partial Tree In mathematics, and more specifically in graph theory, a tree is an undirected graph in ...
- 2015ACM/ICPC亚洲区长春站 H hdu 5534 Partial Tree
Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5534 Partial Tree (完全背包变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...
- hdu 5534 Partial Tree(完全背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- 2015长春 HDU 5534 Partial Tree
题意:有n个结点,n-1条边,现在要把这n个结点连成一棵树,给定了f(i),表示度为i的结点的价值是f(i).现在问如何连能够使得Σf(i)的值最大. 思路:每个点至少一个度,所以可分配的度数为n-2 ...
- HDU 5534 Partial Tree
2015 ACM/ICPC 长春现场赛 H题 完全背包 #include<cstdio> #include<cstring> #include<cmath> #in ...
- hdu 5234 Happy birthday 背包 dp
Happy birthday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
随机推荐
- 【MySQL】Java对SQL时间类型的操作(获得当前、昨天、前年。。时间)
Java获得当前时间 java.util.Date date = new java.util.Date(); Timestamp time = new Timestamp(date.getTime() ...
- js中json与数组字符串的相互转化
<SCRIPT LANGUAGE="JavaScript"> var t="{'firstName': 'cyra', 'lastName': 'richar ...
- spoj 694(后缀数组)
题意:求一个字符串的不重复子串的个数. 分析:对于下标为i的位置,能够产生的前缀子串个数为len-i(下标从0开始),对于与它字典序相邻的后缀产生的子串是重复的(就是他们的最长公共前缀),所以我们要减 ...
- LINQ to SQL语句之Join和Order By
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...
- MVC中modelstate的使用
MVC中ModelState类需要引用 System.Web.Mvc命名空间,在 System.Web.Mvc.dll 中. 属性 Errors 返回一个 ModelErrorCollection 对 ...
- codeforce 702C Cellular Network 二分答案
http://codeforces.com/contest/702 题意:n个村庄,m个机站,问机站最短半径覆盖完所有村庄 思路:直接二分答案 二分太弱,调了半天..... // #pragma co ...
- J2SE7规范_2013.2_类
8.1 类的定义 包括普通类和枚举类,枚举(略) 下面都是指普通类: public只能用于外部类,成员类,不能用于局部类,匿名类 protected和private用于成员类时(待解) sta ...
- hadoop多次格式化后,导致datanode启动不了,怎么办?(伪分布式)
根据当初 hadoop 安装目录下 conf 目录的 core-site.xml 的设置,找到该目录: 进入该目录 在 data 和 name 文件夹下均有 current 文件夹 ,和 cur ...
- lamda表达式相关知识
lamda表达式写法 dt = datado.SelectDalMeath(sqlStr.ToString()); var x = (from r in dt.AsEnumerable() selec ...
- Failed to allocate the network(s), not rescheduling
Failed to allocate the network(s), not rescheduling 在计算节点的/etc/nova/nova.conf中添加下面两句 #Fail instance ...