Optimal Binary Search Tree+区间dp+uva

本文探讨了如何通过区间动态规划方法实现最优二叉搜索树的构建,优化查询元素的路径成本,降低总查找成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem E

Optimal Binary Search Tree

Input: standard input

Output: standard output

Time Limit: 30 seconds

Memory Limit: 32 MB

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 eiof 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(en)

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 

解决方案:此题为区间dp,选择根节点k,dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+sum[k-1]-sum[i-1]+sum[j]-sum[k]),sum为数组前n项和。每个节点到根节点的cost可用区间叠加来代替。
code:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[550][550];
int num[550];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(num,0,sizeof(num));
        for(int i=1; i<=n; i++)
        {
            int temp;
            scanf("%d",&temp);
            num[i]=num[i-1]+temp;
          //  cout<<num[i]<<endl;
        }
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j+i<=n; j++)
            {
                dp[j][j+i]=0x3f3f3f3f;
                for(int k=j; k<=j+i; k++)
                {
                    int temp=0;
                    if(k!=j){
                        temp+=dp[j][k-1]+num[k-1]-num[j-1];
                    }
                    if(k!=i+j){
                        temp+=dp[k+1][j+i]+num[j+i]-num[k];
                    }
                    dp[j][j+i]=min(dp[j][j+i],temp);
                }
            }
        }
        printf("%d\n",dp[1][n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值