码迷,mamicode.com
首页 > 其他好文 > 详细

最优矩阵链乘

时间:2018-02-03 17:47:45      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:矩阵链   cstring   lap   技术   style   none   递推   std   ...   

poj1651

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

技术分享图片
#include<cstdio>
#include<cstring>
#include<algorithm>
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

 

最优矩阵链乘

标签:矩阵链   cstring   lap   技术   style   none   递推   std   ...   

原文地址:https://www.cnblogs.com/DearDongchen/p/8409942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!