思路:

区间dp。

实现:

 class Solution
{
public:
int mctFromLeafValues(vector<int>& arr)
{
int n = arr.size();
vector<vector<int>> maxn(n, vector<int>(n, ));
for (int i = n - ; i >= ; i--)
{
for (int j = i; j < n; j++)
{
if (j == i) maxn[i][j] = arr[i];
else
{
maxn[i][j] = max(maxn[i][j - ], arr[j]);
}
}
}
vector<vector<int>> dp(n, vector<int>(n, INT_MAX));
for (int i = n - ; i >= ; i--)
{
for (int j = i; j < n; j++)
{
if (j == i) dp[i][j] = arr[i];
else if (j == i + ) dp[i][j] = arr[i] * arr[j];
else
{
dp[i][j] = min(dp[i][j], dp[i + ][j] + arr[i] * maxn[i + ][j]);
dp[i][j] = min(dp[i][j], dp[i][j - ] + arr[j] * maxn[i][j - ]);
for (int k = i + ; k < j - ; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + ][j] + maxn[i][k] * maxn[k + ][j]);
}
}
}
}
return dp[][n - ];
}
}

leetcode1130 Minimum Cost Tree From Leaf Values的更多相关文章

  1. LeetCode 1130. Minimum Cost Tree From Leaf Values

    原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...

  2. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  3. 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集

    最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...

  4. Minimum Spanning Tree

    前言 说到最小生成树(Minimum Spanning Tree),首先要对以下的图论概念有所了解. 图 图(Graph)是表示物件与物件之间的关系的数学对象,是图论的基本研究对象.图的定义方式有两种 ...

  5. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...

  6. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  7. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...

  8. CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge

    E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...

  9. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

随机推荐

  1. [AGC028B]Removing Blocks 概率与期望

    考虑算每一个位置在所有情况的期望值乘以全排列似乎就是答案. 那么对于 $i$,如果要由 $j$ 来贡献的话就要满足 $j$ 在 $i....j-1$ 之前先拿. 而在拿 $j$ 时,先于 $i...j ...

  2. Bzoj 2820: YY的GCD(莫比乌斯反演+除法分块)

    2820: YY的GCD Time Limit: 10 Sec Memory Limit: 512 MB Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x& ...

  3. Mxnet框架搭建

    Mxnet框架搭建 小书匠 kindle  Mxnet是亚马逊开发的深度学习框架,和谷歌Tensorflow是同类型的框架. 1.安装Mxnet 这里只展示在线安装,源码编译安装等不演示:GPU安装与 ...

  4. python 的单例

    例子 class Singleton(object): _instance = None def __new__(cls, *args, **kw): if not cls._instance: cl ...

  5. base/7/x86_64/filelists_db FAILED

      解决办法: [root@localhost ~]# cd /var/lib/rpm [root@localhost rpm]# rm -rf __db.* # 清除原 rpmdb 文件 [root ...

  6. docker搭建hadoop HA出错问题总结记录。

    错误1: ssh连接云主机: ssh root@39.106.xx.xx 报错:THE AUTHENTICITY OF HOST XX CAN’T BE ESTABLISHED 解决办法: ssh - ...

  7. CSS 之实现单行、多行文本溢出显示省略号

    一.单行文本省略 实现方法: overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 二.多行文本省略 实现方法: display ...

  8. [信息收集]Nmap命令详解

    0x00[介绍] Nmap,也就是Network Mapper,中文为"网络映射器". Nmap是一款开源的网络探测和安全审核的工具,它的设计目标是快速地扫描大型网络. 它是网络管 ...

  9. NSCTF 2017-pwn2

    目录 程序基本信息 程序漏洞 整体思路 exp脚本 发现的问题 内容参考 程序基本信息 32位动态链接程序,开启了数据段不可执行以及栈溢出保护 程序漏洞 在函数中sub_80487fa中有一个格式化字 ...

  10. Linux中touch命令使用(创建文件)

    touch命令有两个功能: 1.用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来: 2.用来创建新的空文件. 语法 touch(选项)(参数) 选项 -a:或 ...