Triangulation of convex polygon in interval dp--

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.