标签:
类似矩阵连乘的一道题,很简单DP[i][j]表示区间i,j最少费用,
状态转移为DP[i][j] = min{DP[i][k],DP[k][j]} + a[j] - a[i];
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 #define REP(i,N) for (int i = 0;i < (N);i++) 7 #define REP_1(i,N) for (int i = 1;i < (N);i++) 8 #define REP_2(i,be,en) for (int i = (be);i < (en);i++) 9 #define DWN(i,N) for (int i = N;i >= 0;i--) 10 #define INF 0x3f3f3f3f 11 #define MAXN 55 12 13 using namespace std; 14 int L,N; 15 int a[MAXN]; 16 int DP[MAXN][MAXN]; 17 18 int dp(int be,int en) { 19 if (be >= en - 1) return 0; 20 if (DP[be][en]) return DP[be][en]; 21 int MIN = INF; 22 REP_2(i,be + 1,en){ 23 MIN = min(MIN,dp(be,i) + dp(i,en)); 24 } 25 return DP[be][en] = MIN + a[en] - a[be]; 26 } 27 28 int main () { 29 //freopen("1.txt","r",stdin); 30 while (cin >> L,L) { 31 cin >> N; 32 memset(DP,0,sizeof(DP)); 33 REP_1(i,N + 1) { 34 cin >> a[i]; 35 } 36 a[0] = 0;a[N + 1] = L; 37 printf("The minimum cutting is %d.\n",dp(0,N + 1)); 38 } 39 }
标签:
原文地址:http://www.cnblogs.com/xiaoshanshan/p/4295380.html