uva 10003 Cutting Sticks(区间DP)

题目连接:10003 - Cutting Sticks


题目大意:给出一个长l的木棍, 再给出n个要求切割的点,每次切割的代价是当前木棍的长度, 现在要求输出最小代价。


解题思路:区间DP, 每次查找当前区间的最优解 , 并记录。需要注意的是输入的切割点并不是从小到大。


 

#include <stdio.h>

#include <string.h>

#include <algorithm>

using namespace std;

const int N = 55;

int l, n, val[N], dp[N][N];



int count(int a, int b) {

    if (dp[a][b])   return dp[a][b];

    if (a == b - 1) return 0;

    dp[a][b] = 1 << 30;

    for (int i = a + 1; i < b; i++)

	if (count(a, i) + count(i, b) < dp[a][b])

	    dp[a][b] = dp[a][i] + dp[i][b];

    return dp[a][b] += val[b] - val[a];	

}



int main() {

    while (scanf("%d", &l), l) {

	scanf("%d", &n);

	memset(val, 0, sizeof(val));

	memset(dp, 0, sizeof(dp));

	for (int i = 1; i <= n; i++)

	    scanf("%d", &val[i]);

	sort(val, val + n + 1);

	val[n + 1] = l;



	printf("The minimum cutting is %d.\n", count(0, n + 1));

    }

    return 0;

}


 

 

你可能感兴趣的:(uva)