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_动态规划的更多相关文章

  1. LeetCode 1039. Minimum Score Triangulation of Polygon

    原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题目: Given N, consider ...

  2. 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 ...

  3. 【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 ...

  4. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

  5. Leetcode 第135场周赛解题报告

    这周比赛的题目很有特点.几道题都需要找到一定的技巧才能巧妙解决,和以往靠数据结构的题目不太一样. 就是如果懂原理,代码会很简单,如果暴力做,也能做出来,但是十分容易出错. 第四题还挺难想的,想了好久才 ...

  6. Stone Game II

    Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...

  7. UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  8. Quiz(贪心,快速幂乘)

    C. Quiz time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  9. codeforces 337C Quiz(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Quiz Manao is taking part in a quiz. The ...

随机推荐

  1. iOS bounds、frame之间的关系

    这几个都是在ios程序中,经常会注意到的一些小细节,能否真正了解这些,对写ios程序也有很大的好处. frame 是UIView中表示此view的一个矩形面积,包括了view在它的superview中 ...

  2. wcf中序列化BinaryFormatter,DataContractJsonSerializer,DataContractSerializer,SoapFormatter,XmlSerializer

    using System; using System.Runtime.Serialization; using System.Xml.Serialization; namespace Larryle. ...

  3. linux线程笔记1之创建线程

    1 线程与进程的对比 这里有一个笔记详细的阐述 http://blog.csdn.net/laviolette/article/details/51506953 2 创建线程函数 int pthrea ...

  4. Android HandlerThread源码解析

    在上一章Handler源码解析文章中,我们知道App的主线程通过Handler机制完成了一个线程的消息循环.那么我们自己也可以新建一个线程,在线程里面创建一个Looper,完成消息循环,可以做一些定时 ...

  5. POJ2370【水题】

    //#include <bits/stdc++.h> #include<iostream> #include<string.h> #include<cstdi ...

  6. 跳马~~~HDU1372

    基础BFS,水过就好~手写队列优化~~ #include <iostream> #include <stdio.h> #include <string.h> #in ...

  7. Codeforces 2 A. Winner

    哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈....... 先让我笑完................ 就是一道撒比题啊,一开始是题目看错= =.是,但是后面还是自己不仔细错的.....不存在题目坑这种情况 ...

  8. Struts+Spring+Hibernate处理Lob(Blob,Clob)

    在使用struts+spring+hibernate的开发中,有些时候用户会有数据库存储文件的需求,在数据库中一般会采用Blob字段或Clob字段来存储二进制图片.流媒体或文件.现就将在实际开发中遇到 ...

  9. 手机测试用例-STK测试用例

    ID 功能描述 操作步骤 预期结果 test time P/F comment tester test time P/F comment tester STK服务 SIM卡适应性测试 1.选取支持ST ...

  10. jQuery笔记之工具方法

    jQuery 工具方法 $.type()判断数据类型 $.isArray() $.isFunction() $.isWindow()... $.trim()消除空格 $.proxy()改变this指向 ...