POJ 1651 Multiplication Puzzle (矩阵连乘)

#include <stdio.h>
#define MAX_CARDS 100

int numOfCards;
int cardNum[MAX_CARDS + 1];
int minScore[MAX_CARDS + 1][MAX_CARDS + 1];

int main(){

	scanf("%d", &numOfCards);
	int card;
	for (card = 1; card <= numOfCards; card++)
		scanf("%d", &cardNum[card]);

	int cards, start, mid, end;
	for (cards = 2; cards < numOfCards; cards++){
		for (start  = 1; start < numOfCards; start++){
			end = start + cards;
			if (end > numOfCards)
				continue;
			minScore[start][end] = 1e7;
			for (mid = start + 1; mid < end; mid++){
				int score = minScore[start][mid] + cardNum[start] * cardNum[mid] * cardNum[end] + minScore[mid][end];
				if (score < minScore[start][end])
					minScore[start][end] = score;
			}
		}
	}

	printf("%d\n", minScore[1][numOfCards]);

	return 0;
}

你可能感兴趣的:(poj,Multiplication,Puzzl,1651,DP动态规划)