AYITACM2016 Province third week i-optimal Array multiplication Sequence (DP)

Source: Internet
Author: User

Matrix least multiplication


Test instructions

Give you 2 matrices A, B, we use the standard matrix multiplication definition c=ab as follows:

The number of columns in a array (column) must be equal to the number of column (row) in the B array to multiply this 2 array. If we use rows (a), columns (a), respectively, to represent the number of columns and bars in the A array, the number of multiplication required for the C array to be calculated is: Rows (a) *columns (B) *columns (a). For example: A array is a 10x20 matrix, b array is a 20x15 matrix, then to calculate the C array needs to do 10*15*20, that is, 3,000 times multiplication.

To calculate the multiplication of more than 2 matrices, you have to decide what order to use. For example: X, Y, z are matrices, there are 2 options to calculate XYZ: (XY) Z or X (YZ). Suppose X is an array of 5x10, Y is an array of 10x20, Z is an array of 20x35, and the number of multiplication required for that different sequence of operations is different:

(XY) Z

    • 5*20*10 = 1000 times The multiplication is done (XY), and a 5x20 array is obtained.
    • 5*35*20 = 3,500 times The final result is obtained.
    • Total number of multiplication required: 1000+3500=4500.

X (YZ)

    • 10*35*20 = 7,000 times The multiplication is completed (YZ), and a 10x35 array is obtained.
    • 5*35*10 = 1750 times The final result is obtained.
    • Total number of multiplication required: 7000+1750=8750.

Obviously, we can see that the calculation (XY) z uses fewer times of multiplication.

The question is: give you some matrices, you have to write a program to decide how to multiply the order, so that the number of times to use the multiplication will be the least.

Idea: the optimal matrix chain multiplication problem, the typical dynamic programming topic.


#include <stdio.h> #define M 1000#define inf 0x3f3f3f3fint dp[m][m],s[m][m],p[m],n;void print (int i,int j)//        Recursively outputs all results {if (i==j) printf ("a%d", I);//output is the number of data else {printf ("(");        Print (i,s[i][j]);//recursive Next data printf ("X");        Print (S[I][J]+1,J);    printf (")");    }}void work () {int i,j,k,l,t;            for (I=1; i<=n; i++) dp[i][i]=0;//first initialized to 0 for (l=2; l<=n; l++) for (i=1; i<=n-l+1; i++) {         J=I+L-1;            Determine the optimal solution dp[i][j]=inf between I and J;                for (k=i; k<=j-1; k++) {t=dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j];  if (T<dp[i][j]) {dp[i][j]=t; Maintain optimal solution s[i][j]=k; Record where parentheses are required}}} print (1,n); The optimal solution between the output I and J puts ("");}    int main () {int c=1,i; while (scanf ("%d", &n) &&n) {for (i=1; i<=n; i++) scanf ("%d%d", &p[i-1],&p[i]);//Outputinto the data printf ("Case%d:", C + +);    Work (); } return 0;}


AYITACM2016 Province third week i-optimal Array multiplication Sequence (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.