Title: http://poj.org/problem?id=1651
Water problem
Test instructions: A number of cards, the score of the first card is a[i] (1 <= i <= N). Now the game is as follows, given n cards, in addition to the first and last card is not available, you can take one, get the score a[i–1] * a[i] * a[i + 1], and then the card removed. The title requires the final calculation of the minimum total score.
Principle: Matrix multiplication.
1#include <iostream>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5 using namespacestd;6 Long Longdp[ the][ the],a[ the],tmp;7 8 intMain ()9 {Ten intn,i,j,k,t,len,m,tmp; One while(SCANF ("%d", &n)! =EOF) A { - for(i=0; i<n; i++) -scanf"%d",&a[i]); theMemset (DP,0,sizeof(DP)); - for(len=1; len<=n-1; len++) - for(i=1, j=len+1; j<=n-1; i++,j++) - { +m=999999999; - for(k=i;k<=j-1; k++) + { Atmp=dp[i][k]+dp[k+1][j]+a[i-1]*a[k]*A[j]; at if(tmp<m) -m=tmp; - } -d[i][j]=m; - } -printf"%lld\n", dp[0][n-1]); in } - return 0; to}
Interval Dp-poj1651-multiplication Puzzle