最优矩阵链乘

poj1651

按区间长度递增的顺序递推...

#include
#include
#include
using namespace std;
const int maxn = 105, inf = 0x3f3f3f3f;
int a[maxn], dp[maxn][maxn];
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    n--;
    for(int len = 0; len <= n-1; len++){
        for(int i = 1; i+len <= n; i++){
            int j = i+len;
            if(i == j)
                dp[i][j] = 0;
            else{
                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]);
                }
            }
        }
    }
    printf("%d\n", dp[1][n]);
    return 0;
}
/*
 6
 10 1 50 50 20 5
*/
View Code

 

转载于:https://www.cnblogs.com/DearDongchen/p/8409942.html

你可能感兴趣的:(最优矩阵链乘)