Cod sursa(job #2024489)

Utilizator patricia.predaPatricia Preda patricia.preda Data 20 septembrie 2017 18:52:58
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
#include <iostream>
#include <cstdio>
#define INF (1LL << 62)

using namespace std;
int n;
long long d[505], matrice[505][505];
void citire()
{
    scanf("%d", &n);
    for(int i=0; i<=n; i++)
        scanf("%lld", &d[i]);
}

void parcurgere()
{
    for(int i=1; i<n; i++)
        matrice[i][i + 1] = d[i - 1] * d[i] * d[i + 1];
    for (int w=2; w<n; w++)
        for (int i= 1; i<=n-w; i++)
        {
            int j=i+w;
            matrice[i][j] = INF;
            for (int k= i; k<= j - 1; k++)
            {
                long long suma= matrice[i][k] + matrice[k + 1][j] +  d[i - 1] * d[k] * d[j];
                matrice[i][j]=min(matrice[i][j], suma);
            }
        }

    /*for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            printf("%lld ", matrice[i][j]);
        printf("\n");
    }*/
     printf("%lld", matrice[1][n]);
}

int main()
{
    freopen("podm.in","r",stdin);
    freopen("podm.out","w",stdout);
    citire();
    parcurgere();
    return 0;
}