poj1651 optimal matrix multiplication dynamic programming problem solving

Source: Internet
Author: User

Title Description:

There are several matrix {Ai}, and the elements are all integers and the matrix size is known.

If you want to calculate the product of all matrices A1 * A2 * A3: Am, at least how many times the integer multiplication?

Input
The first line is an integer n (n <= 100), which indicates that there is a total of n-1 matrices.
Second row n integers B1, B2, B3 ... Bn (BI <= 100), the number of I-bi represents the number of rows of the first matrix and the columns of the i-1 matrix.
Equivalently, the number of rows for the J-Matrix AJ (1 <= J <= n-1) is BJ and the number of columns is bj+1.
Output
An integer that represents the minimum number of multiplication times required
Using Dynamic programming Analysis:
The goal of optimization is to minimize the number of basic operations, and the key is to determine the order of the multiplication, which is equivalent to the parentheses between the n matrices. How to define the boundary of sub-problem is the problem that arises. If the former is divided, the resulting sub-problem has only the posterior boundary, but in the process of computing sub-problem A1.....J, we need to know the sub-problem a1....i and sub-Problem A (i+1) .... J Information, so the problem requires a front and back two boundaries. Use M[i][j] to represent the minimum number of operations used by the product AI....J. Assuming that the last multiplication occurred between AI.....K and a (k+1) .... J, then M[i][j]=min (i≤k<j) {m[i][k]+m[k+1][j]+b (i-1) *b (k) *b (j)};
Below is the C language implementation of the Code
#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;Const intmax=10000;intm[101][max];intdpint*p,intN) {     for(intR=2; r<=n;r++){         for(intI=1; i<=n-r+1; i++){            intj=i+r-1; M[I][J]=m[i+1][j]+p[i-1]*p[i]*P[j];  for(intk=i+1; k<=j-1; k++){                intt= m[i][k]+m[k+1][j]+p[i-1]*p[k]*P[j]; if(t<M[i][j]) {M[i][j]=T; }            }        }    }    returnm[1][n];}intMain () {intN; scanf ("%d",&N); memset (M,0,sizeof(m)); intp[ -];  for(intI=0; i<n;i++) {scanf ("%d", p+i); }    intRES=DP (p,n-1); printf ("%d\n", RES); return 0;}

The algorithm has a time complexity of O (N3).

poj1651 optimal matrix multiplication dynamic programming problem solving

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.