Question link: uva-10304 question to a sequence can be S = (e1, e2 ,..., and e1 <e2 <.. <en. these sequences must form a binary search tree. The binary search tree is recursive. If its left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root node. If its right subtree is not empty, the value of all nodes on the right tree is greater than the value of its root node. In practical applications, elements with higher access frequency should be closer to the root node to save time. Each element has an access frequency f (ei). When the element is located in a location with a depth of k, cost (ei) = k. sum = f (e1) * cost (e1) + f (e2) * cost (e2) +... + f (en) * cost (en) We call the binary search tree with the smallest sum value as the optimal binary search tree. Returns the set sequence S in sequence, and the frequency f (ei) of each element. The minimum value of sum is obtained because the sequence given by his question is small to large, if any ei of this sequence is set as the root node, we can know that all the numbers on the left of ei in the sequence will constitute the left subtree of ei, all the numbers on the right of the ei will constitute the right subtree of the ei. Then we can enumerate the root node and select a scheme with the smallest value. Speaking of this, combined with the data range of the question, it is easy to think of the interval dp! Set f (I, j) to the value of an optimal binary search tree consisting of the number of sequential intervals (I, j). When the root node ek is enumerated, its left subtree (wi, wi + 1 ,.., wk-1), the depth of all nodes increases by 1, then the left subtree increases by sum (w1, w2 ,... wk-1) right subtree (ek + 1, ek + 2 ,.. the sum (ek + 1, ek + 2 ,..., j ). it can be seen that the sum (I, j)-wk will be added in total, then the state transfer can be introduced: f (I, j) = min {f (I, k-1) + f (k + 1, j) + sum (I, j)-wk | I <= k <= j} code
/** =================================================== =================== * This is a solution for ACM/ICPC problem ** @ source: uva-10304 Optimal Binary Search Tree * @ description: interval dp * @ author: shuangde * @ blog: blog.csdn.net/shuangde800 * @ email: zengshuangde@gmail.com * Copyright (C) 2013/09/06 All rights reserved. * ===================================================== ===================*/# include <iostream> # include <Cstdio> # include <algorithm> # include <vector> # include <queue> # include <cmath> # include <cstring> using namespace std; typedef long int64; const double PI = acos (-1.0); const int INF = 0x3f3f3f3f; const int MAXN = 210; int n; int w [MAXN]; int sum [MAXN]; int f [MAXN] [MAXN]; int main () {while (~ Scanf ("% d", & n) {sum [0] = 0; for (int I = 1; I <= n; ++ I) {scanf ("% d", & w [I]); sum [I] = sum [I-1] + w [I];} memset (f, 0, sizeof (f); for (int d = 2; d <= n; ++ d) {for (int l = 1; l + d-1 <= n; + + l) {int r = l + d-1; int ans = INF, tot = sum [r]-sum L-1]; for (int k = l; k <= r; ++ k) ans = min (ans, f [l] [k-1] + f [k + 1] [r] + tot-w [k]); f [l] [r] = ans ;}} printf ("% d \ n", f [1] [n]);} return 0 ;}