标签:dp
3 1 2 3 7 13 7 8 16 21 4 18
9 239
区间dp,设dp[i][j]表示合并第i堆石子导第j堆石子所花的最小代价,那么dp[i][j] = min(dp[i][k] + dp[k + 1][j] + sum[i][j])
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 220; const int inf = 0x3f3f3f3f; int w[N]; int dp[N][N]; int sum[N]; int main() { int n; while (~scanf("%d", &n)) { sum[0] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); sum[i] = sum[i - 1] + w[i]; } memset (dp, 0, sizeof(dp)); for (int i = n; i >= 1; i--) { for (int j = i + 1; j <= n; j++) { int tmp = inf; for (int k = i; k < j; k++) { tmp = min(tmp, dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1]); } dp[i][j] = tmp; } } printf("%d\n", dp[1][n]); } return 0; }
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/40952823