uva 10304 Optimal Binary Search Tree 最优二叉树编码 区间dp

题意:给出一组频率,让你建立一个二叉树,计算每个节点的频率×层数,使得这个值最小,问最小的值是多少。


设f(i, j)表示序列区间(i, j)的数构成的一棵最优二叉查找树的值,
   当枚举根节点ek时,它的左子树(wi,wi+1,..,wk-1)的所有节点的深度都会增加1,
   那么左子树增加sum(w1,w2,...wk-1)
   右子树(ek+1, ek+2,..ej)的值也会增加sum(ek+1,ek+2,...,ej).
   可以看出,那么总共会增加sum(i, j) - wk

---fromshangde大神

状态转移方程就是:dp[i][j] = min(dp[i][k - 1] + dp[k + 1] + sum(i, j) - a[i])

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  Blog:        http://blog.csdn.net/hcbbt
*  File:        uva10304.cpp
*  Create Date: 2013-11-12 22:32:08
*  Descripton:  invertel dp 
*/

#include <cstdio>
#include <cstring>

#define min(a, b) ((a) < (b) ? (a) : (b))

const int MAXN = 210;
int n, a[MAXN], sum[MAXN];
int dp[MAXN][MAXN];

int main() {
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			sum[i] = sum[i - 1] + a[i];
		}

		for (int d = 2; d <= n; d++)
			for (int l = 1; l + d - 1 <= n; l++) {
				int r = l + d - 1;
				dp[l][r] = 0x7ffffff;
				for (int i = l; i <= r; i++)
					dp[l][r] = min(dp[l][r], dp[l][i - 1] + dp[i + 1][r] + sum[r] - sum[l - 1] - a[i]);
			}

		printf("%d\n", dp[1][n]);
	}
	return 0;
}


你可能感兴趣的:(ACM)