Cod sursa(job #2251062)

Utilizator LivcristiTerebes Liviu Livcristi Data 1 octombrie 2018 09:20:26
Problema Parantezare optima de matrici Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <fstream>
#define NUM 505
#define INF 1000000000LL
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long lint;
lint mat[NUM][NUM];
lint d[NUM];
int n;
using namespace std;
int main()
{
    ifstream f("podm.in");
    ofstream g("podm.out");
    f >> n;
    for(int i = 0; i <= n; ++i)
        f >> d[i];
    for(int i = 1; i <= n; ++i)
        mat[i][i] = 0;
    for(int i = 1; i <= n - 1; ++i)
        mat[i][i + 1] = d[i - 1] * d[i] * d[i + 1];

    for(int w = 2; w <= n - 1; ++w)
    {
        for(int i = 1; i <= n - w; ++i)
        {
            int j = i + w;
            mat[i][j] = INF;
            for(int k = i; k <= j - 1; ++k)
                mat[i][j] = Min(mat[i][j], mat[i][k] + mat[k + 1][j] + d[i - 1] * d[k] * d[j]);
        }
    }
    g << mat[1][n];
    f.close();
    g.close();
}