Cod sursa(job #1344704)

Utilizator superman_01Avramescu Cristian superman_01 Data 16 februarie 2015 22:19:45
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <fstream>
#include <algorithm>
#include <vector>
#include <iostream>

#define NMAX 505

#define get_max(a,b) ((a)>(b)?(a):(b))
#define get_min(a,b) ((a)<(b)?(a):(b))

using  namespace std;

ifstream in ( "podm.in" );
ofstream out ( "podm.out" );

long long DP[NMAX][NMAX] , D[NMAX] ;
int N ;

int main ( void ){
  int i , j;
  in >> N ;
  for ( i = 0 ; i <= N ; ++i )
  in >> D[i];
  for ( i = 1 ; i < N ; ++i )
    DP[i][i+1] = D[i-1] * D[i] * D[i+1];

  for ( int step = 2 ; step < N ; ++step )
    for ( i = 1 ; i <= N - step ; ++i ){
         j = i + step ;
    DP[i][j] = (1LL<<62);
    for ( int k = i  ; k < j ; ++ k )
    DP[i][j] = get_min(DP[i][k] + DP[k+1][j] + D[i-1]*D[k]*D[j], DP[i][j]);
    }

 out << DP[1][N];
 return 0;
}