Cod sursa(job #410907)

Utilizator DraStiKDragos Oprica DraStiK Data 4 martie 2010 17:23:48
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <algorithm>
using namespace std;

#define INF 1LL<<60
#define DIM 505

long long best[DIM][DIM];
int d[DIM];
int n;

void read ()
{
    int i;

    scanf ("%d",&n);
    for (i=0; i<=n; ++i)
        scanf ("%d",&d[i]);
}

void init ()
{
    int i,j;

    for (i=1; i<=n; ++i)
        for (j=1; j<=n; ++j)
            best[i][j]=INF;
}

void calc (int x,int y)
{
    int i;

    if (x==y)
    {
        best[x][y]=0;
        return ;
    }
    if (x+1==y)
    {
        best[x][y]=(long long)d[x-1]*d[x]*d[x+1];
        return ;
    }
    else
        for (i=x; i<y; ++i)
        {
            if (best[x][i]==INF)
                calc (x,i);
            if (best[i+1][y]==INF)
                calc (i+1,y);
            best[x][y]=min (best[x][y],best[x][i]+best[i+1][y]+(long long)d[x-1]*d[i]*d[y]);
        }
}

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

    read ();
    init ();
    calc (1,n);
    printf ("%lld",best[1][n]);

    return 0;
}