Given a convex polygon with n (n<50) vertices (numbered from 1 to n), the weights of each vertex are known. Ask how to divide this convex polygon into N-2 triangles, so that the sum of the weights of the vertices of these triangles is the smallest one?
Input file: Number of vertices in first row n
Weights for n vertices in the second row (from 1 to N)
Output format: Minimum and value
Example: 5
1 2 3) 4 5
Output: 38
1 /*did not find the OJ website, I built a few sets of data to try it2 the transfer equation of interval DP: The beginning and extension length of the interval is generally involved .3 F[i][j] represents the minimum product sum of the interval of a J-length triangle from I, and the endpoint i,i+j-1 can be a triangle with any point within the range that is not adjacent to I,i+j-1, so enumerating the intermediate points K can*/4#include <iostream>5 using namespacestd;6#include <cstdio>7#include <cstring>8 #defineN 1209 #defineMAX (1<<31)-1Ten Long LongA[n]; One Long LongF[n][n]; A intN; - intMain () - { thescanf"%d",&n); - for(intI=1; i<=n;++i) - { -scanf"%d",&a[i]); +A[i+n]=a[i];/*Turn this polygon into a two-fold chain.*/ - } +Memset (F,127,sizeof(f)); A for(intI=1; i<=2*n-2;++i) atf[i][3]=a[i]*a[i+1]*a[i+2];/*Initialize*/ - for(intj=4; j<=n;++j) - for(intI=1; i+j<=2*n+1;++i) - for(intk=i+2; i+j-k>=3; ++k)/*Enumeration K*/ -F[i][j]=min (f[i][j],f[i][k-i+1]+f[k][i+j-k]+a[i]*a[k]*a[i+j-1]); - Long Longans=MAX; in for(intI=1; i<=n;++i)/*Note that at the end, all points are enumerated as the interval end length n, and the minimum value is found .*/ -ans=min (ans,f[i][n]); tocout<<ans<<Endl; + return 0; -}
Triangulation of convex polygon in interval dp--