leetcode_1039. Minimum Score Triangulation of Polygon_动态规划
https://leetcode.com/problems/minimum-score-triangulation-of-polygon/
题意:给定一个凸的N边形(N<=50),每个顶点有一个权值A[i],把它分为N-2个三角形,每个三角形的val等于三个顶点的权值的乘积,问划分之后图形的val总和最小为多少。
一开始想到了,问题可以转换为求解子问题,由于没有想到如何进行状态转换,并且感觉贪心可行,
如下图1,将当前图形可以构成的三角形找出(红线为底边),从中找到val最小的三角形如图2,然后将其割除如图3,再将新的两个三角形找出。


然而这个思路并不可行,这个思路的局部最优并不能得到全局最优,按照上面的思路的策略如下图,得到结果40,而最优解是24。

贪心不可行,每次找到最优的三角形,最终全局未必最优。
然后,想到很可能是动态规划,但是想不出来,问题如何分解,状态如何表示。。。
参考别人的思路和代码(看了两个大佬的代码,思路几乎一样)
对于多边形上的任意一条边,它只能出现在一个三角形中,
dp[i][j]:从第i个点到第j个点的最小值。dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+A[i]*A[j]*A[k])。
如下图:此时求dp[0][5],以(0,5)为边,在(0,5)之间遍历三角形的顶点,构成的每个三角形将原图形分为三部分:三角形,子图形1,子图形2。

class Solution
{
public:
int minScoreTriangulation(vector<int>& A)
{
int dp[][];
memset(dp,,sizeof(dp));
int numofnode = A.size();
for(int len=; len<=numofnode; len++)
for(int i=; i+len-<numofnode; i++)
{
int j=i+len-;
dp[i][j]=INT_MAX;
for(int k=i+; k<j; k++)
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]+A[i]*A[j]*A[k]);
}
return dp[][A.size()-];
}
};
leetcode_1039. Minimum Score Triangulation of Polygon_动态规划的更多相关文章
- LeetCode 1039. Minimum Score Triangulation of Polygon
原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题目: Given N, consider ...
- Minimum Score Triangulation of Polygon
Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwi ...
- 【leetcode】1039. Minimum Score Triangulation of Polygon
题目如下: Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in c ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Leetcode 第135场周赛解题报告
这周比赛的题目很有特点.几道题都需要找到一定的技巧才能巧妙解决,和以往靠数据结构的题目不太一样. 就是如果懂原理,代码会很简单,如果暴力做,也能做出来,但是十分容易出错. 第四题还挺难想的,想了好久才 ...
- Stone Game II
Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- Quiz(贪心,快速幂乘)
C. Quiz time limit per test 1 second memory limit per test 256 megabytes input standard input output ...
- codeforces 337C Quiz(贪心)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Quiz Manao is taking part in a quiz. The ...
随机推荐
- IOS AutoLayout 代码实现约束—VFL
在autolayout下,尽管使用IB来拖放控件,但仍然避免不了用代码来创建控件,这是约束需要代码来实现. IOS 提供了两种添加约束的方法 第一种: +(instancetype)constrain ...
- iOS成员变量、实例变量、属性变量三者的联系与区别
一.类Class中的属性property 在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是 ...
- [POI 2007] 办公楼
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1098 [算法] 显然 , 答案为补图的连通分量个数 用链表优化BFS , 时间复杂度 ...
- Spring中Bean获取IOC容器服务的方法
Spring 依赖注入可以让所有的Bean对其IOC容器的存在是没有意识的,甚至可以将容器换成其它的.但实际开发中如果某个Bean对象要用到Spring 容器本身的功能资源,需要意识到IOC容器的存在 ...
- 【转】git修改文件后,提交到远程仓库
原文地址:https://blog.csdn.net/nly19900820/article/details/73613654 修改文件后,怎么提交到远程仓库1.git status 查看git是否有 ...
- springmvc不进入Controller导致404
转自:https://blog.csdn.net/qq_36769100/article/details/71746449#1.%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE ...
- 整合ssh的时候出现空指针java.lang.NullPointerException
转自:https://blog.csdn.net/koudailidexiaolong/article/details/9468857 HTTP Status 500 - type Exception ...
- Task运行带参数的函数
Task<Int32> task = Task.Run(() => fun("s", 9)); 函数定义: private Int32 frun(string ...
- B. Blown Garland
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 03. 服务注册和管道
ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 03. 服务注册和管道 语雀: https://www.yuque.com/yuejiangliu/dotnet/ ...