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 */