Uva--10304(区间DP)

2014-08-03 14:59:13

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 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(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

思路:以最优二叉搜索树为背景的题目,借鉴了别人的代码,A掉了人生第一道区间DP题。。
核心思想就两部分:
(1)DP状态转移方程:dp[i][j]表示将区间[i,j]内的树构成OBST的最优解,dp[i][j] = min(dp[i][k-1] + dp[k+1][j] + sum[i][j] - f[k]) (i < k < j)
(2)用递归来写当然可以,但是要写成递推式的话区间DP思想是免不了的,先求出区间长度最小(len==1)的最优解,再根据这些最优解求长度次小(len==2)的最优解,依次类推到区间长度
最长(len==n)的最优解。来总结一下这题区间dp的结构:

for p = 1 to n (所枚举的区间长度)
  for i = 1 to n - p + 1 (枚举区间起点)
    j = i + p - 1 (定义区间终点)
  for k = i to j (枚举区间[i,j]的根节点位置)
    dp[i][j] = min(dp[i][k-1] + dp[k+1][j] + sum[i][j] - f[k]) (状态转移方程)
 1 /*************************************************************************
 2     > File Name: c.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Sun 03 Aug 2014 01:44:19 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int INF = 1e9;
16 
17 int n;
18 int v[255];
19 int sum[255];
20 int dp[255][255];
21 
22 int main(){
23     while(scanf("%d",&n) == 1){
24         scanf("%d",&v[1]);
25         sum[1] = v[1];
26         sum[0] = 0;
27         for(int i = 2; i <= n; ++i){
28             scanf("%d",&v[i]);
29             sum[i] = sum[i - 1] + v[i];
30         }
31         memset(dp,0,sizeof(dp));
32         for(int p = 2; p <= n; ++p){ //枚举区间长度
33             for(int i = 1; i <= n - p + 1; ++i){ //枚举区间起点
34                 int j = i + p - 1; //区间终点
35                 dp[i][j] = INF;
36                 for(int k = i; k <= j; ++k){
37                     dp[i][j] = min(dp[i][j],dp[i][k - 1] + dp[k + 1][j] + sum[j] - sum[i - 1] - v[k]);
38                 }
39             }
40         }
41         printf("%d\n",dp[1][n]);
42     }
43     return 0;
44 }

 

posted @ 2014-08-03 15:27  Naturain  阅读(208)  评论(0编辑  收藏  举报