hdu5534Partial Tree(长春现场赛H题)
2015-11-01 21:58
211 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5534
大意就是一颗n个点的树,每个点度数不同,权值不同,问你怎么构建这棵树,使这棵树权值最大。
这道题就是DP ,DP i,j i表示有i个点,j 表示 最后有j 个点在一个父节点上。
dp[0][0]=n*f
.
http://acm.hdu.edu.cn/showproblem.php?pid=5534
大意就是一颗n个点的树,每个点度数不同,权值不同,问你怎么构建这棵树,使这棵树权值最大。
这道题就是DP ,DP i,j i表示有i个点,j 表示 最后有j 个点在一个父节点上。
dp[0][0]=n*f
.
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int INF=1000000000; int T,n,c[3000],f[2050][2050]; int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n-1;i++) scanf("%d",&c[i]); for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) f[i][j]=-INF; f[0][0]=n*c[0]; for(int i=1;i<=n-2;i++) { for(int j=1;j<=n;j++) f[i][j]=max(f[i][j],f[i-1][j-1]+c[j]-c[j-1]); int res=-INF; for(int j=0;j<=n;j++) res=max(res,f[i-1][j]); f[i][1]=res+c[1]-c[0]; } int res=-INF; for(int i=0;i<=n;i++) res=max(res,f[n-2][i]); printf("%d\n",res); } return 0; }
相关文章推荐
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- hdu-5385
- hdu-5405
- hdu-5490
- HDU 1622 Trees On The Level
- HDU 1063 Exponentiation
- 关于在一定刀数内切一个圆的球,求出切出最多块数的值的数学推论 hdoj 1290