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