POJ 1651:multiplication Puzzle Matrix Multiply-DP

Source: Internet
Author: User

Multiplication Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7118 Accepted: 4385

Description

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During The move player takes one card out of the row and scores the number of points equal to the product of the The card taken and the numbers on the cards in the left and on the it. It is not a allowed to take out the first and the last card in the row. After the final move, only the cards is left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 1 5, player might take a card with 1, then and a, scoring
10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000
If he would take the cards in the opposite order, i.e. and then 1, the score would is
1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input

The first line of the input contains the number of cards n (3 <= n <= 100). The second line contains N integers in the range from 1 to, separated by spaces.

Output

Output must contain a single integer-the minimal score.

Sample Input

610 1 50 50 20 5

Sample Output

3650

Test instructions is to give a few numbers, take the middle of the number, each take the number I will have a corresponding cost value[i-1]*value[i]*value[i+1], asked is the given sequence to take away all the middle of the cost of the least amount.

is the matrix multiplication dp ... Put that number to think of it as the first to take it away is OK, ah, how their brains always ran to the dead end of the dead, gas me.

Code:

#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string > #include <cstring> #pragma warning (disable:4996) using namespace std; #define INF 0x3f3f3f3fint dp[105][105]; int Value[105];int Num;int Main () {int i,j,k,len;scanf ("%d", &num), for (i=1;i<=num;i++) {scanf ("%d", &value[ I]);DP [i][i]=0;} for (i=1;i<=num;i++) {dp[i][i+1] = 0;dp[i][i+2] = value[i]*value[i+1]*value[i+2];} for (len=3;len<=num;len++) {for (i=1,j=len+i;j<=num;i++) {j=len+i;dp[i][j]=inf;for (k=i+1;k<j;k++) {dp[i][j ]=min (Dp[i][j],dp[i][k]+dp[k][j]+value[k]*value[i]*value[j]);//imagined to be the first to take away}}}cout<<dp[1][num]<<endl ; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1651:multiplication Puzzle Matrix Multiply-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.