dp最优矩阵相乘poj1651

题目链接

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int INF = 1e9;
 7 const int maxn = 1000+5;
 8 int dp[maxn][maxn],n,p[maxn];
 9 
10 int solve(int i,int j){
11     if(dp[i][j] != INF) return dp[i][j];
12     if(j == i+1) return 0;
13 
14     for(int k=i+1; k<j; k++){
15         dp[i][j] = min(dp[i][j],p[i]*p[k]*p[j] + solve(i,k) + solve(k,j));
16     }  
17 
18     return dp[i][j];
19 }
20 
21 int main(){
22     cin >> n;
23     for(int i=0; i<n; i++)
24         cin >> p[i];
25     for(int i=0; i<n; i++)
26         for(int j=0; j<n; j++)
27             dp[i][j] = INF;
28 
29     cout << solve(0,n-1) << endl;
30 }
原文地址:https://www.cnblogs.com/yxg123123/p/6827748.html