Cod sursa(job #3148237)

Utilizator thinkphpAdrian Statescu thinkphp Data 30 august 2023 06:07:24
Problema Parantezare optima de matrici Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>

//Matrix Chain Multiplication

int dim[ 10 ], n;

long cost[ 10 ][ 10 ];

void mcm(int n, int dim[ 10 ]) {

      int j;

      for(int i = 1; i <= n; i++) cost[i][i] = 0;

      for(int k = 1; k <= n - 1; ++k)

          for(int i = 1; i <= n - k; i++) {

               j = i + k;

               cost[i][j] = 1<<30;

               for(int q = i; q <= j - 1; q++) {

                 long m = cost[i][q] + cost[q+1][j] + dim[i] * dim[q+1] * dim[j+1];

                 if(cost[i][j]>m) {

                   cost[i][j] = m;

                   cost[j][i] = 1;
                 }
               }
      }

      std::cout<<cost[1][n];
}

int main(int argc, char const *argv[]) {

  freopen("podm.in","r",stdin);
	freopen("podm.out","w",stdout);


  std::cin>>n;

  for(int i = 1; i <= n + 1; i++) std::cin>>dim[i];
  //10 1 10 1 10
  mcm(n,dim);

  return 0;
}