您的位置:首页 > 其它

HDU 5534 Partial Tree(dp 背包)

2017-10-13 19:14 381 查看

题目链接

Partial Tree

分析

 总共 2(n−2) 度, 度数为 i 价值是f[i], 先给每个点一度,那麽剩余 n-2 个点分配就好,这不就是背包吗

 但是很奇怪的是,我用滚动数组倒着dp的时候wa了????为什么???

AC code

#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define PI acos(-1)
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define INF64 0x3f3f3f3f3f3f3f3f
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define ms(x,v) memset((x),(v),sizeof(x))
#define eps 1e-8
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
typedef unsigned long long uLL;
typedef long double DB;
typedef pair<int,int> Pair;
const int maxn = 1e5+10;
const int  MOD = 1e9+7;

int dp[maxn];
int a[maxn];
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int T;
cin>>T;
while (T--) {
int n;
cin>>n;
for(int i=1 ; i<n ; ++i)cin>>a[i];
ms(dp,-INF);
for(int i=2 ; i<n ; ++i)a[i]-=a[1];
dp[0] =a[1]*n;

for(int i=1 ; i<=n-2 ; ++i){
for(int j = i ; j<=n-2 ; ++j){
dp[j] = max(dp[j],dp[j-i]+a[i+1]);
}
// for(int j=n-2 ; j>=i ; --j)
//     dp[j] = max(dp[j],dp[j-i]+a[i+1]);
}
std::cout << dp[n-2]<< '\n';
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp