Polygon triangulation

Polygon triangulation

Description

N convex polygon vertices [vertex order as 1-> N], each vertex weights is known, requires divided into triangles N-2, the product so that the value of each apex of the triangle is the smallest weight when n = 4, each vertex weights were 10,5,7,6 when the required minimum value is 10 . 5 . 6 + 5 . 6 . 7 = 510.

Input

The first line: second line an integer n: n integers, respectively for the weight of each vertex

Output

It represents a minimum integer sum of the products

Sample Input

4
10 5 7 6

Sample Output

510

HINT

n <= 200, all the input data are <= 1000, is less than the determined minimum of 10 ^ 9.

Source

#include <bits/stdc++.h>
using namespace std;
int n;
int a[201];
long long f[201][201];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    memset(f,0x20,sizeof(f));
    for(int i=1;i<=n;i++)
    f[i][i]=f[i][i+1]=0;
    for(int len=3;len<=n;len++)
         for(int i=1;i<=n;i++)
         {
            int j=i+len-1;
            if(j>n) break;
            for(int k=i+1;k<=j-1;k++)
                if(f[i][j]>f[i][k]+f[k][j]+a[i]*a[j]*a[k])
                f[i][j]=f[i][k]+f[k][j]+a[i]*a[j]*a[k];
         } 
         cout<<f[1][n]<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/11922112.html