UVA 10304 Optimal Binary Search Tree

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA 10304 Optimal Binary Search Tree相关的知识,希望对你有一定的参考价值。

简单区间DP。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn = 300;
int n;
int a[maxn], sum[maxn], dp[maxn][maxn];

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

        memset(dp, 0, sizeof dp);

        for (int i = 2; i <= n; i++)
        {
            for (int j = 1; j+i-1 <= n; j++)
            {
                int st = j, en = j + i - 1; 
                int ans = 0x7FFFFFFF;
                for (int k = st; k <= en; k++)
                    ans = min(dp[st][k - 1] + dp[k + 1][en] + sum[en] - sum[st - 1] - a[k],ans);
                dp[st][en] = ans;
            }
        }
        printf("%d\n", dp[1][n]);
    }
    return 0;
}

 

以上是关于UVA 10304 Optimal Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

uva 10304 - Optimal Binary Search Tree 区间dp

uva10304-最优排序二叉树

uva1349Optimal Bus Route Design

ITA 15.5 Optimal binary search trees

[Coding Made Simple] Optimal Binary Search Tree

UVA1349 Optimal Bus Route Design 拆点法+最小费用最佳匹配