HDU 5534 完全背包
Partial Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 823 Accepted Submission(s): 407
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?
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.
3
2 1
4
5 1 4
19
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
int f[maxn];
int dp[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int ans = ;
scanf("%d",&n);
for(int i = ; i<n; i++) scanf("%d",&f[i]);
ans = n*f[];
for(int i = n-; i>; i--) f[i] -= f[];
for(int i = ; i<=n-; i++)
dp[i] = -inf;
dp[] = ;
for(int i = ; i <= n-; i++) //因为1度已定,所以从2枚举度数
for(int j = ; j <= n-; j++)
if(j>=i-) dp[j] = max(dp[j],dp[j-i+]+f[i]);
printf("%d\n",ans+dp[n-]);
}
return ;
}
HDU 5534 完全背包的更多相关文章
- hdu 5534 (完全背包) Partial Tree
题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, ...
- H - Partial Tree HDU - 5534 (背包)
题目链接: H - Partial Tree HDU - 5534 题目大意:首先是T组测试样例,然后n个点,然后给你度数分别为(1~n-1)对应的不同的权值,然后问你在这些点形成树的前提下的所能形 ...
- HDU 1011 树形背包(DP) Starship Troopers
题目链接: HDU 1011 树形背包(DP) Starship Troopers 题意: 地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...
- hdu 5445 多重背包
Food Problem Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- hdu 1203 01背包 I need a offer
hdu 1203 01背包 I need a offer 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 题目大意:给你每个学校得到offe ...
- HDU 5534 Partial Tree (完全背包变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题意: 给你度为1 ~ n - 1节点的权值,让你构造一棵树,使其权值和最大. 思路: 一棵树上 ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- hdu 5534 Partial Tree(完全背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...
- HDU 5534 Partial Tree 完全背包
一棵树一共有2*(n-1)度,现在的任务就是将这些度分配到n个节点,使这n个节点的权值和最大. 思路:因为这是一棵树,所以每个节点的度数都是大于1的,所以事先给每个节点分配一度,答案 ans=f[1] ...
随机推荐
- PAXOS may not terminate
It’s easy to see that Paxos does have a failure mode. When two proposers are active at the same time ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- drupal7 分页
$output = ""; $query = db_select('feedback','f')->extend('PagerDefault');//->extend( ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- hiho 1015 KMP
input 1<=T<=20 string1 1<=strlen(string1)<=1e4 string2 2<=strlen(string2)<=1e6 out ...
- pkgmgmt: Comparison between different Linux Systems..
found this page.. already done by precedents.. installation: aptitude install apt-get install yum in ...
- [转]于Fragment和Activity之间onCreateOptionsMenu的问题
Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于 ...
- svn 设置文件可执行权限
本地文件在commit到仓库之前若没有chmod +x 权限的话,那在svn仓库里的文件将会保持当前无可执行属性状态. 即使在本地chmod +x filename 之后,再提交到仓库也是没有用的.c ...
- 【转载】GDI 映像方式 之 SetViewportExtEx 与 SetWindowExtEx 解析
所谓视口代表设备,比如屏幕. 窗口代表我们的思维. 我们对windows说在(5,6)处画个点(调用GDI函数).windows认为是在我们的思维的(5,6)处画了个点.(也就是说5,6是逻辑坐标,G ...
- 利用apache组件实现文件上传
实现文件上传需要引入: commons-fileupload-1.3.2.jar commons-io-2.5.jar commons-logging-1.2.jar <!DOCTYPE htm ...