leetcode1130 Minimum Cost Tree From Leaf Values
思路:
区间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的更多相关文章
- LeetCode 1130. Minimum Cost Tree From Leaf Values
原题链接在这里:https://leetcode.com/problems/minimum-cost-tree-from-leaf-values/ 题目: Given an array arr of ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- 最小生成树(Minimum Spanning Tree)——Prim算法与Kruskal算法+并查集
最小生成树——Minimum Spanning Tree,是图论中比较重要的模型,通常用于解决实际生活中的路径代价最小一类的问题.我们首先用通俗的语言解释它的定义: 对于有n个节点的有权无向连通图,寻 ...
- Minimum Spanning Tree
前言 说到最小生成树(Minimum Spanning Tree),首先要对以下的图论概念有所了解. 图 图(Graph)是表示物件与物件之间的关系的数学对象,是图论的基本研究对象.图的定义方式有两种 ...
- 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...
- 使用jQuery快速高效制作网页交互特效---JavaScript对象及初始面向对象
一.JavaScript中的基本数据类型 number(数值类型) string(字符串类型) boolean(布尔类型) null(空类型) undefined(未定义类型) ...
- html5文件夹上传源码
前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...
- 内存原理与PHP的执行过程
一.内存结构 栈区:保存的是变量名(术语:引用),对于cpu来说,读写速度很快 堆区:存储“复杂”的数据,数组.对象.字符串(字符串比较特殊)等 数据段:又分为数据段全局区(用于存储简单的数据,如数字 ...
- 一个milller_rabin模板
#include <iostream> #include <cstdlib> #include <stdio.h> #include <algorithm&g ...
- [Bob]Collectors Problem
https://vjudge.net/problem/UVA-10779#author=0 网络流 1.Bob向他有的贴纸连边,流量为他有的贴纸数量 2.每一种贴纸向汇点连流量为1的边 3.其余人,如 ...
- pyCharm专业版破解方案【附:四种破解】
前言: 一般适合我们的工具才是好的工具,通过调研对比发现pycharm还不错,其它工具就不一一列举了 pyCharm社区版可以永久免费,但是它不支持HTML, JS, and SQL等,为了方便以后使 ...
- known_hosts有什么用?
一.问题描述 当我连接我的服务器的时候,返回信息如下 二.问题分析 返回的信息是什么意思? IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! ...
- 如何计算一个C/C++程序运行时间
前两天要计算一个用C++实现的算法运行时间,就用了clock()这个函数.程序大体上如下: clock_t start,end; start = clock(); /*my code*/ end = ...
- docker-compose 布署应用nginx中的create-react-app应用获取环境变量
文章来源:https://www.freecodecamp.org/news/how-to-implement-runtime-environment-variables-with-create-re ...