Cod sursa(job #838324)

Utilizator Tester66Tester66 Tester66 Data 19 decembrie 2012 12:53:22
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;


#define nmax 505
#define inf 100000000000000000LL
#define ll long long


ll Best[nmax][nmax], d[nmax];
int n;


int main()
{
    freopen("podm.in", "r", stdin);
    freopen("podm.out", "w", stdout);
    int i, j, k, w;
    scanf("%i", &n);
    for(i = 0; i <= n; i++) scanf("%i", &d[i]);
    for(i = 1; i <= n; i++) Best[i][i] = 0;
    for(i = 1; i < n; i++) Best[i][i + 1] = d[i - 1] * d[i] * d[i + 1];
    for(w = 2; w <= n - 1; w++)
    {
          for(i = 1; i <= n - w; i++)
          {
                int j = i + w;
                Best[i][j] = inf;
                for(k = i; k <= j - 1; k++)
                      Best[i][j] = min(Best[i][j], Best[i][k] + Best[k + 1][j] + d[i - 1] * d[j] * d[k]);
          }
    }for(i = 1; i <= 100000000; i++);
    printf("%lld\n", Best[1][n]);

    return 0;
}