码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10304 Optimal Binary Search Tree

时间:2016-06-26 11:34:40      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

简单区间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

标签:

原文地址:http://www.cnblogs.com/zufezzt/p/5617472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!