Algorithm: uva-10304 Optimal Binary Search tree (interval dp)

Source: Internet
Author: User

Meaning

Give a sequence can S = (e1,e2,..., en), and e1<e2< <en. To make these sequences into a binary search tree.

The binary search tree is recursive in nature, and if its left subtree is not empty, the value of all nodes in the left subtree is less than the value of its root node; if it

Right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node of the tree.

Because in practical applications, the more frequently accessed elements, the closer the root node should be, so as to save more time to find.

Each element has an access frequency of f (EI), and the cost (EI) = k, where the element is at a depth of K.

The sum of the cost and frequency of access for all nodes is:

sum = f (E1) *cost (E1) + F (E2) *cost (E2) + ... + f (en) *cost (en)

We call the sum value of the smallest two-fork search tree for the optimal binary search tree.

Give the set sequence s, and the frequency f (EI) of each element in order, to find the minimum value of sum
Ideas

Because his topic gives a sequence that is small to large, then for any of the EI of this sequence, set EI 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, and all the numbers on the right of EI will constitute

The right subtree of EI.

Then we can enumerate the root nodes and then choose a scheme with the least value.

When it comes to combining the data range of the topic, it's easy to think of the interval DP!

Set F (i, j) to represent the value of an optimal binary lookup tree consisting of the number of sequence intervals (I, j).

When enumerating the root node ek, the depth of all nodes of its Zuozi (wi,wi+1,.., wk-1) is increased by 1.

Then the left subtree adds sum (w1,w2,... wk-1)

Right subtree (ek+1, ek+2,.. EJ) also increases the sum (ek+1,ek+2,..., EJ).

As you can see, you'll add sum (i, j)-WK

Then you can launch a state transfer:

F (i, j) = min{f (i,k-1) +f (k+1,j) +sum (i, j)-wk | i<=k<=j}

Code

/**===================================================== * 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 16:37 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> # Include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring
    
> Using namespace std;
typedef long 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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.