题目大意:
http://codevs.cn/problem/1048/
基本思路:
dp[i][j]代表把i到j堆的堆在一起所需要的最小花费。dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+wi+....+wj)。
代码:
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int arr[110]; int dp[110][110]; int sum[110]; const int INF=0x7fffffff; int main() { memset(arr,0,sizeof(arr)); memset(sum,0,sizeof(sum)); int n; cin >> n; for(int i = 1; i <= n; i++) { int tmp; cin >> tmp; arr[i] = tmp; sum[i] = sum[i-1]+tmp; } for(int i = 1; i <= n; i++) { for(int j = i -1; j >= 0; j--) { dp[j][i] = INF; for(int k = j; k < i; k++) { dp[j][i] = min(dp[j][i], dp[j][k]+dp[k+1][i]+sum[i]-sum[j-1]); } } } cout << dp[1][n] << endl; return 0; }