Optimal matrix chain multiplication

Source: Internet
Author: User

The main large interval into the inter-district ...

First small interval evaluation ...

State transfer equation F (i,j) = min{F (i,k) + f (k+1,j) + p[i-1]p[k]p[j]};

POJ 1651 http://poj.org/problem?id=1651

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>Const intMAXN = -+Ten;Const DoubleESP = 10e-8;Const DoublePi = Atan (1.0) *4;Const intINF =0xFFFFFFF;Const intMOD =10000007;using namespacestd;intN;intDP[MAXN][MAXN];intC[MAXN];voidmultiplication () { for(inti =0; I < n;i++) {Dp[i][i]=0; }     for(intL =2; l < n;l++){         for(inti =1; I < n-l+1; i++){            intj = i+l-1; DP[I][J]=INF;  for(intK = I;k < j;k++){                intTMP = Dp[i][k] + dp[k+1][J] + c[i-1] * C[k] *C[j]; DP[I][J]=min (dp[i][j],tmp); }        }    }}intMain () {//freopen ("Input.txt", "R", stdin);scanf"%d",&N);  for(inti =0; I < n;i++) {scanf ("%d",&C[i]);    } multiplication (); printf ("%d\n", dp[1][n-1]); return 0;}
View Code

POJ 1179 http://poj.org/problem?id=1179

Approximate test instructions:

Give you a ring, the point is the number, the edge is an operation, you break one of the edges, and then you can combine the two connected points through the side of the operation into a point, to find the maximum value

To solve the problem, enumerate the broken points and multiply the matrices.

The ... 1 I can't copy the code ... Copying the code is not working ... Orz ... http://m.blog.csdn.net/blog/u012962816/26822959

The ... 2 Http://www.tuicool.com/articles/Jj2Avi

When the first one is stupid enough to copy the wrong, refer to the 2

Then I wrote it from the new ...

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>Const intMAXN = -+Ten;Const DoubleESP = 10e-8;Const DoublePi = Atan (1.0) *4;Const intINF =0xFFFFFF;Const intMOD =10000007;using namespacestd;CharEDGE[MAXN];intVET[MAXN];intdp1[maxn/2][maxn/2],dp2[maxn/2][maxn/2];intFIN[MAXN];intN;intMain () {//freopen ("Input.txt", "R", stdin);    intAns =-INF; scanf ("%d",&N);  for(inti =0; I < n;i++) {GetChar (); scanf ("%c%d",&edge[i],&Vet[i]); }     for(intt =0; t < n;t++) {//from the T position as the starting point         for(inti =0; i < n;i++) {//initialization of the matrixDp1[i][i] = vet[(t+i)%N]; Dp2[i][i]=Dp1[i][i]; }           for(intL =2; l <= n;l++) {//Length of J-i             for(inti =0; I < n-l+1; i++){                 intMAXV =-INF; intMINV =INF; intj = i+l-1;  for(intK = I;k < j;k++){                    if(edge[(k +1+t)%N] = ='T'){//It 's going to change here, too.MAXV = Max (Maxv,dp1[i][k] + dp1[(k +1)%N]                        [j]); MINV= Min (Minv,dp2[i][k] + dp2[(k +1)%N]                    [j]); }                    Else{MAXV= MAX (maxv,dp1[i][k] * dp1[(k +1)%N]                        [j]); MAXV= MAX (maxv,dp2[i][k] * dp2[(k +1)%n][j]);//negative Negative positiveMINV = min (minv,dp1[i][k] * dp2[(k +1)%n][j]);//positive and negative multiplicationMINV = min (minv,dp2[i][k] * dp2[(k +1)%N]                        [j]); MINV= MIN (minv,dp2[i][k] * dp1[(k +1)%n][j]);//positive and negative multiplication}} Dp1[i][j]= MAXV;//to assign a valueDP2[I][J] =MINV; }} ans= Max (ans,dp1[0][n-1]); Fin[t]= dp1[0][n-1]; } printf ("%d\n", ans);  for(inti =0; I < n;i++){        if(Fin[i] = =ans) {printf ("%d", i+1); }} printf ("\ n"); return 0;}

UVA 10003 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& category=847&problem=944&mosmsg=submission+received+with+id+15274403

D (I,J) is the best cost for i-j, then D (i,j)-min{D (i,k) + D (k,j) + a[j]-a[i]};

First small interval evaluation ...

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<cstdlib>Const intMAXN = ++Ten;Const DoubleESP = 10e-8;Const DoublePi = Atan (1.0) *4;Const intINF =0xFFFFFF;Const intMOD =10000007;using namespacestd;intA[MAXN];intDP[MAXN][MAXN];intMain () {//freopen ("Input.txt", "R", stdin);    intLen;  while(~SCANF ("%d", &len) &&Len) {        intN; scanf ("%d",&N);  for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); } memset (DP,0,sizeof(DP)); a[0] =0; A[n+1] =Len;  for(intL =2; l <= n+1; l++){             for(inti =0; I+l <= n+1; i++){                intj = i+l; DP[I][J]=INF;  for(intK = i+1; k < j;k++){                    intTMP = Dp[i][k] + dp[k][j] + a[j]-A[i]; if(TMP <Dp[i][j]) {Dp[i][j]=tmp; } }}} printf ("The minimum cutting is%d.\n", dp[0][n+1]); }    return 0;}

Optimal matrix chain multiplication

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.