uva10304- optimal binary search tree

Meaning of the questions: for each tree node you are looking for frequency F, then spent a node is K * F, K is the root node to the current path length. The total cost of the tree is the sum (Ki * Fi).

I ask, how much is the minimum cost tree.

Original title link

Problem-solving ideas:

Interval DP, it takes only a root node is first calculated, is 0, then the computational expense of each two consecutive nodes. Calculating the cost per three consecutive nodes, and finally the cost of the entire tree.

Set DP [i] [j] represents the subscript i to the cost table j, then dp [i] [j] = min (dp [i] [k-1] + dp [k + 1] [j] + k is to take root);

K is to take root = sum [i .... k-1] + sum [k + 1 ..... j].

Such thinking, in the following standard K is the root to get the time, dp [i] [k-1] and dp [k + 1] [j] are certainly optimal binary search tree cost.

If the root K is equivalent to the height of the tree + 1, i.e., the path becomes long, and, at this time should be spent subtracting k.

Code corresponding 

int Total = SUM [E] - SUM [S - . 1];
int
Val = DP [S] [K - . 1 ] + DP [K + . 1 ] [E] + Total - NUM [K];
SUM is a prefix and, total internal contains num [k], so it should be minus

 

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main
{
    static final int N = 253;
    //static final int N = 5;
    static int dp[][] = new int[N][N];
    static int num[] = new int[N];
    static int sum[] = new int[N];
    static int INF = (int) 1e6;
    static int n;

    public static void main(String[] args) throws FileNotFoundException
    {
         Scanner scanner = new Scanner(System.in);
        //Scanner scanner = new Scanner(new File("/Users/caicai/in"));
        while (scanner.hasNextInt())
        {
            initdp();
            n = scanner.nextInt();
            for (int i = 1; i <= n; i++)
            {
                num[i] = scanner.nextInt();
                sum[i] = sum[i - 1] + num[i];
            }
            dp();
        }
    }

    static void initdp()
    {
        for (int i = 0; i < N; i++)
        {
            sum[i] = 0;
            for (int j = 0; j < N; j++)
            {
                dp[i][j] = 0;
            }
        }

    }

    static void dp()
    {
        for (int len = 1; len < n; len++)
        {
            for (int i = 1;; i++)
            {
                int s = i;
                int e = i + len;
                if (e > n) break;
                int total = sum[e] - sum[s - 1];
                int ans = INF;
                for (int k = s; k <= e; k++)
                {
                    int val = dp[s][k - 1] + dp[k + 1][e] + total - num[k];
                    if (ans > val) ans = val;
                }
                dp[s][e] = ans;
            }
        }
        System.out.println(dp[1][n]);
    }

}

 

 

 

 

 

 

topic:

 

Given a set S = (e1, e2, ..., en) of n distinct elements such that e1 < e2 < . . . < en and considering a binary search tree (see the previous problem) of the elements of S, it is desired that higher the query frequency of an element, closer will it be to the root. The cost of accessing an element ei of S in a tree (cost(ei)) is equal to the number of edges in the path that connects the root with the node that contains the element. Given the query frequencies of the elements of S, (f(e1), f(e2), . . . , f(en)), we say that the total cost of a tree is the following summation:

f (e1) * cost (e1) + f (e2) * cost (e2) +. . . + F (en) * cost (one)

In this manner, the tree with the lowest total cost is the one with the best representation for searching elements of S. Because of this, it is called the Optimal Binary Search Tree.

 

Input

The input will contain several instances, one per line.

Each line will start with a number 1 ≤ n ≤ 250, indicating the size of S. Following n, in the same line, there will be n non-negative integers representing the query frequencies of the elements of S: f(e1), f(e2), . . . , f(en), 0 ≤ f(ei) ≤ 100. Input is terminated by end of file.

Output

For each instance of the input, you must print a line in the output with the total cost of the Optimal Binary Search Tree.

 

Sample Input

1 5

3 10 10 10

3 5 10 20

Sample Output

0

20

20

 

Guess you like

Origin www.cnblogs.com/shuiyonglewodezzzzz/p/11311004.html