Cod sursa(job #429232)

Utilizator jeanFMI - Petcu Ion Cristian jean Data 29 martie 2010 22:46:32
Problema Parantezare optima de matrici Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include<stdio.h>
#define Inf 1ll<<60
#define Nmax 510

long long A[Nmax][Nmax],i,j,k,n,l,col,x;

struct matrice {long long x,y;} M[Nmax];

int main()
{
	freopen("podm.in","r",stdin);
	freopen("podm.out","w",stdout);
	
	scanf("%d",&n);
	scanf("%d",&l);
	
	for(i=1;i<=n;i++,l=col)
	{
		scanf("%lld",&col);
		M[i].x=l; M[i].y=col;
	}
	if(n==1) {printf("0"); return 0;}
	
	for(l=n-1;l;l--)
		for(i=l,j=n;i;i--,j--)
		{
			A[i][j]=Inf;
			
			for(k=i;k<j;k++)
			{
				x = A[i][k] + A[k+1][j] + M[i].x*M[k].y*M[j].y;
				if(x < A[i][j]) A[i][j]=x;
			}
		}
	printf("%lld",A[1][n]);
return 0;
}