Cod sursa(job #2380707)

Utilizator marian013Giugioiu Marian Constantin marian013 Data 15 martie 2019 13:40:15
Problema Parantezare optima de matrici Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("podm.in");
ofstream g("podm.out");
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define INF   100000000000000000LL
long long bst[505][505],d[505];
int n;
int main()
{
    f>>n;
    for(int i=0;i<=n;i++)
        f>>d[i];
    for(int i=1;i<=n;i++)
        bst[i][i]=0;
    for(int i=1;i<n;i++)
        bst[i][i+1]=d[i-1]*d[i]*d[i+1];
    for(int e=2; e<n;e++)
    for(int i=1; i<=n - e; i++)
    {
        int j = i + e;
        bst[i][j] = INF;
        for(int k=i;k<j;k++)
            bst[i][j] = Min(bst[i][j], bst[i][k] + bst[k + 1][j] + d[i - 1] * d[k] * d[j]);
    }
    g<<bst[1][n];
}