Cod sursa(job #1222565)

Utilizator Luncasu_VictorVictor Luncasu Luncasu_Victor Data 23 august 2014 16:49:19
Problema Parantezare optima de matrici Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX 505

struct Matrix{
	int X, Y;
	Matrix(){
	}
	Matrix(int X, int Y) {
		this->X = X, this->Y = Y;
	}
} M[MAX];
int N;
unsigned long long Best[MAX][MAX], B;

int main() {
	int X, Y;

	freopen("podm.in","r",stdin);
	freopen("podm.out","w",stdout);
	
	scanf("%d", &N);	
	scanf("%d", &X);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &Y);
		M[i] = Matrix(X,Y);
		X=Y;
	}

	for (int j = 2; j <= N; j++)
		for (int i = 1; i <= N - j + 1; i++) {
			for (int k = i; k < j + i - 1; k++) {
				B = Best[i][k] + Best[k+1][j+i-1] + M[i].X * M[k].Y * M[j+i-1].Y;
				if (Best[i][j+i-1] == 0 || (B < Best[i][j+i-1])) {
					Best[i][j+i-1] = B;
				}
			}
		}
	
	cout << Best[1][N];
	
	return 0;
}