很基础的一道区间DP :)
#include <cstdio> #include <iostream> #define INF 0x3f3f3f3f using namespace std; int c[1005]; int dp[1005][1005]; int main () { int l, n; while(scanf("%d", &l)!=EOF && l) { scanf("%d", &n); for(int i=1; i<=n; i++) { scanf("%d", &c[i]); } c[0]=0; c[++n]=l; for(int step=2; step<=n; step++) { for(int i=0; i+step<=n; i++) { int j = i+step; dp[i][j] = INF; for(int k=i+1; k<j; k++) { dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+c[j]-c[i]); } } } printf("The minimum cutting is %d.\n", dp[0][n]); } return 0; }
时间: 2024-10-12 15:38:52