Title Description
0 Kawasaki has a lot of friends, one of which is called JHLJX.
Jhljx Everyone is very familiar with, he is not good at maths is also famous, we all understand.
Now that JHLJX has encountered matrix multiplication, he was ignorant. How can matrix multiplication be made clear by the number of countless? Although the zero-Saitama think it might as well let you to calculate, but at least also to give JHLJX a face, give her a chance to prove their math strength. In order to reduce the amount of JHLJX, let him quickly calculate the incorrect answer (it is estimated that he will not be able to make a correct answer a few times), 0 Kawasaki please help JHLJX.
Input
Multiple sets of input data.
Each set of data starts with N and represents the length of the matrix chain. The next line n+1 number represents the number of rows/columns of the matrix.
1<=n<=300
Output
For each set of samples, the output line of the minimum number of operations of the scheme, every two matrix multiplication is "()" enclosed, see the sample
If there are multiple solutions, the final output selection calculates the left matrix first, as described in hint
Input sample
310 30 5 60310 20 5 4
Output sample
((A1A2)A3)((A1A2)A3)
Hint
For the second set of data entered,
If the calculation Order is ((A1A2) A3), the result is 10x20x5 + 10x5x4= 1200,
If the calculation Order is A1 (A2A3), the result is 20x5x4 + 10x20x4 = 1200
Then the output selects the first
Title Source: Http://biancheng.love/contest/17/problem/D/index
Topic Note: If there are multiple solutions, the final output selection calculates the left matrix first
The implementation code is given below:
1#include <bits/stdc++.h>2 #defineMax_size 4003 #defineINF 1000000004 Long LongS[max_size][max_size];//preserving structure optimal solution information5 Long LongP[max_size];//Record of matrix size6 Long LongM[max_size][max_size];//Record optimal values7 8 voidMatrix_chain_order (intN)9 {Ten for(intI=1; i<=n;i++) One { Am[i][i]=0;//Initialize optimal value (starting at 1, ending at N) - } - for(intL=2; l<=n;l++)//L Indicates the length of the matrix the { - for(intI=1; i<=n-l+1; i++) - { - intj=i+l-1; +m[i][j]=INF; -s[i][j]=0; + for(intk=j-1; k>=i;k--)//the solution takes precedence over the left-first matrix A { at intq=m[i][k]+m[k+1][j]+p[i-1]*p[k]*P[j]; - if(q<M[i][j]) - { -m[i][j]=Q; -s[i][j]=K; - } in } - } to } + } - the voidPrint_optimal_parents (intIintJ//Print the results of the best solution * { $ if(i==j)Panax Notoginsengprintf"a%d", i); - Else the { +printf"("); A print_optimal_parents (I,s[i][j]); thePrint_optimal_parents (s[i][j]+1, j); +printf")"); - } $ } $ - intMain () - { the intN; - while(~SCANF ("%d",&N))Wuyi { theMemset (P,0,sizeof(P)); - for(intI=0; i<=n;i++) Wu { -scanf"%lld",&p[i]); About } $ Matrix_chain_order (n); -Print_optimal_parents (1, n); -printf"\ n"); - } A}
For a detailed explanation of the implementation process, see http://www.cnblogs.com/Anker/archive/2013/03/10/2952475.html
Matrix Chain Multiplication Instance