还是很弱 二叉树啊 没忘区间dp想啊 搜解题报告了
使劲戳这里吧
http://blog.csdn.net/hcbbt/article/details/15776963
看懂了就写了个记忆化搜索
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 255; int a[maxn]; int sum[maxn]; int dp[maxn][maxn]; int dfs(int l, int r) { if(dp[l][r] != -1) return dp[l][r]; if(l > r) return 0; dp[l][r] = 999999999; for(int i = l; i <= r; i++) { dp[l][r] = min(dp[l][r], dfs(l, i-1)+dfs(i+1, r)+sum[r]-sum[l-1]-a[i]); } return dp[l][r]; } int main() { int n; while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum[i] = sum[i-1]+a[i]; } memset(dp, -1, sizeof(dp)); printf("%d\n", dfs(1, n)); } return 0; }
UVa 10304 Optimal Binary Search Tree / 区间DP,布布扣,bubuko.com
UVa 10304 Optimal Binary Search Tree / 区间DP
原文:http://blog.csdn.net/u011686226/article/details/20945047