1.题目链接。矩阵链乘最优次数。区间DP。第一层枚举数量,第层枚举起点,第三层枚举中间位置。
#include<iostream>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 110;
int a[N], dp[N][N];
const int INF = 1e9 + 10;
#pragma warning(disable:4996)
int main()
{
int n;
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
memset(dp, 0, sizeof(dp));
for (int l = 2; l < n; l++)
{
for (int i = 2; i + l <= n + 1; i++)
{
int j = i + l - 1;
dp[i][j] = INF;
for (int k = i; k < j; k++)
{
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + a[i - 1] * a[k] * a[j]);
}
}
}
cout << dp[2][n] << endl;
}
}