POJ 1651 interval DP

Source: Internet
Author: User

Test instructions: Give a set of n number, each time to draw a number (the first and the last can not be drawn), the score is the number of extraction and the two adjacent number of the product. Until the first two digits are left. Q What is the minimum score?

Link: Point Me

Transfer equation:

DP[I][J]=DP[I][K]+DP[K][J]+A[I]*A[J]*A[K]

The k here must be the last step, so multiply a[i] and a[j]

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; AtypedefLong Longll; - #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); the Const intmaxn=1005; - intN,m,tt; - intA[MAXN],DP[MAXN][MAXN]; - intMain () + { -     inti,j,k; + #ifndef Online_judge AFreopen ("1.in","R", stdin); at     #endif -      while(SCANF ("%d", &n)! =EOF) -     { -          for(i=0; i<n;i++) -         { -scanf"%d", A +i); in         } - CL (DP); to          for(i=0; i<n;i++) +              for(j=i+2; j<n;j++) dp[i][j]=INF; -          for(i=0; i<n-2; i++) the         { *dp[i][i+2]=a[i]*a[i+1]*a[i+2]; $         }Panax Notoginseng          for(intlen=3; len<n;len++) -         { the              for(i=0; i+len<n;i++) +             { Aj=len+i; the                  for(k=i+1; k<=j-1; k++) +                 { -Dp[i][j]=min (dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[j]*a[k]); $                 } $             } -         } -printf"%d\n", dp[0][n-1]); the     } -}

POJ 1651 interval DP

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.