Given a convex polygon with n vertices (numbered 1 to n clockwise), the weights of each vertex are known. Ask how to divide this convex polygon into N-2 disjoint triangles, making the sum of the weights of the vertices of these triangles a minimum.
F[I][J] represents the smallest weight from the numbered I to J (clockwise) after the successive vertices are divided; then the problem solved becomes f[1][n]
The code is as follows:
#include <iostream>
#include <cstdio>
long long w[55],f[55][55];
int main () {
long long n,i,x,k,j;
scanf ("%i64d", &n);
for (i=1;i<=n;i++) scanf ("%i64d", &w[i]);
for (x=2;x<n;x++) //consecutive vertex count for
(i=1;i<=n-x;i++) { //starting from the first vertex of the
j=i+x;
f[i][j]=999999999999999999ll;//initial value should be large enough for
(k=i+1;k<j;k++)
f[i][j]=min (F[i][j],f[i][k]+f[k][j]+w[i] *W[K]*W[J]);
}
printf ("%i64d", F[1][n]);
}
nkoj1463
Similar to Nkoj 2305 cut cake