Chinaunix首页 | 论坛 | 博客
  • 博客访问: 71302
  • 博文数量: 41
  • 博客积分: 1475
  • 博客等级: 上尉
  • 技术积分: 440
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 22:49
文章分类
文章存档

2012年(8)

2011年(1)

2009年(32)

我的朋友
最近访客

分类: C/C++

2012-03-23 17:39:23

题目: 

思路: (递归,DP)
  1. 记binary search tree的N个节点分别为 1, 2, 3, ..., N
  2. 记frequency[n]为第n个节点的frequency值
  3. 记sumFrequency(l,r)为从l到r的节点的frequency和。
  4. 记minTreeCost(i,l,r)是root节点为i, 所有节点为l到r的BST的cost,其中l <= i <= r。
    minTreeCost(i,l,r) = minCost(l,i-1) + sumFrequency(l,i-1) + minCost(i+1,r) + sumFrequency(i+1,r)
  5. 记minCost(l,r)为节点为l,l+1,l+2,...r的BST的最小cost (1 <= l,r <= N)。那么我们所求的BST的最小cost就可以表示为minCost(1,N)。可以看出,minCost(l,r)是个不变值,因此我们可以用一个数组记下来,不必重复计算,这就是DP。
    minCost(l,r) = MIN { minTreeCost(l,l,r), minTreeCost(l+1,l,r),..., minTreeCost(r,l,r) }
代码:

点击(此处)折叠或打开

  1. //
  2. // Author: xfye
  3. // Status: AC
  4. //

  5. #include <iostream>

  6. using namespace std;

  7. // g_cost[i][j] is the minimum cost of the binary search tree from
  8. // i to j. This tree is the topmost tree.
  9. int g_minNativeCost[250][250];

  10. //
  11. // Description
  12. // Get the native cost of a binary search tree whose nodes are
  13. // from l to r;
  14. //
  15. // Parameters
  16. // @frequency frequency array
  17. // @l The left index in frequency array
  18. // @r The right index in frequency array
  19. //
  20. // Return
  21. // Returns the minimum native cost of the tree (from l to r);
  22. //
  23. // Remasks
  24. // The tree has a native cost when the level of the root is 0.
  25. // The native cost of this tree (from l to r) is saved into
  26. // g_minNativeCost[l][r] for later using (DP).
  27. // The full cost of a tree is calculated like this:
  28. // fullCost = nativeCost + (f1 + f2 + ...fn) * level
  29. //
  30. //
  31. int findMinNativeCost(int frequency[], int l, int r)
  32. {
  33.     if (l == r) {
  34.         return 0;
  35.     }

  36.     int minNativeCost = -1;

  37.     //
  38.     // find the minimum cost for each tree whose root is i
  39.     //
  40.     for (int i = l; i <= r; i++) {
  41.         // int nativeCost = frequency[i] * level;
  42.         int nativeCost = 0;

  43.         if (i != l) {
  44.             if (g_minNativeCost[l][i-1] == -1) {
  45.                 nativeCost += findMinNativeCost(frequency, l, i-1);
  46.                 for (int k = l; k <= i-1; k++) {
  47.                     nativeCost += frequency[k];
  48.                 }
  49.                 g_minNativeCost[l][i-1] = nativeCost;
  50.             } else {
  51.                 nativeCost += g_minNativeCost[l][i-1];
  52.             }
  53.         }
  54.         if (i != r) {
  55.             if (g_minNativeCost[i+1][r] == -1) {
  56.                 nativeCost += findMinNativeCost(frequency, i+1, r);
  57.                 for (int k = i+1; k <= r; k++) {
  58.                     nativeCost += frequency[k];
  59.                 }
  60.                 g_minNativeCost[i+1][r] = nativeCost;
  61.             } else {
  62.                 nativeCost += g_minNativeCost[i+1][r];
  63.             }
  64.         }

  65.         if (minNativeCost == -1) {
  66.             minNativeCost = nativeCost;
  67.         } else if (minNativeCost > nativeCost) {
  68.             minNativeCost = nativeCost;
  69.         }
  70.     }

  71.     return minNativeCost;
  72. }

  73. int main()
  74. {
  75.     int n = 0;
  76.     int frequency[250];

  77.     while (cin >> n) {
  78.         for (int i = 0; i < n; i++) {
  79.             cin >> frequency[i];
  80.         }

  81.         for (int i = 0; i < 250; i++) {
  82.             for (int j = 0; j < 250; j++) {
  83.                 g_minNativeCost[i][j] = -1;
  84.             }
  85.         }
  86.         cout << findMinNativeCost(frequency, 0, n-1) << endl;
  87.     }

  88.     return 0;
  89. }

阅读(865) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~