Cutting Sticks (区间DP)(区间DP递推详解)

原题目:

You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they only make one cut at a time. It is easy to notice that different selections in the order of cutting can led to different prices. For example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end. There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6. Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 = 20, which is a better price. Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.
Input
The input will consist of several input cases. The first line of each test case will contain a positive number l that represents the length of the stick to be cut. You can assume l < 1000. The next line will contain the number n (n < 50) of cuts to be made. The next line consists of n positive numbers ci (0 < ci < l) representing the places where the cuts have to be done, given in strictly increasing order. An input case with l = 0 will represent the end of the input.
Output
You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of cutting the given stick. Format the output as shown below.
Sample Input
100 3 25 50 75 10 4 4 5 7 8 0
Sample Output
The minimum cutting is 200. The minimum cutting is 22.

中文概要:

(紫书--p278--例9-9)

选择合适的顺序切给定的n个点。怎么选择切割的成本最低,每次切割的成本等于棍子的长度。所以切割的顺序影响最后的结果。明显这是需要动态规划减少决策的规模。


#include<stdio.h>

#include<string.h>

#include<algorithm>

using namespace std;

#define INF 0x3f3f3f3f

int dp[60][60],n[60];

int main()

{

    int l,m,p,i,j,k,t;

    while(scanf("%d",&t)&&t)

    {

        scanf("%d",&m);

        for(i=1; i<=m; i++)

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

        n[0]=0;

        n[m+1]=t;

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

        for(p=1; p<=m+1; p++)//p是区间的可变结束点

            for(i=0; i<=m+1; i++) //i是区间的开始

            {

                int j=i+p;//i是开始,j是结束

                int mi=INF;

                if(j>m+1) //如果结束点超过了木棍长度,跳出循环

                    break;

                //如果区间内没有小区间是无法被合并的,最优解就是区间长度

                for(k=i+1; k<j; k++)  //从最小的区间向大的区间依次合并

                {

                    int tm=dp[i][k]+dp[k][j]+n[j]-n[i];

                    mi=min(mi,tm); //每个区间保持最小值,最优解

                }

                if(mi!=INF)

                    dp[i][j]=mi;   //找到从i到j的最优解

            }

        printf("The minimum cutting is %d.\n",dp[0][m+1]);

    }

    return 0;

}

思路:

我们逆向思维,考虑将一根根短木棍拼接成一个长木棍的情况。 
 
 
那么如上图,取第0块和第1块,第1块和第2块,第2块和第3块的代价很容易计算出来是4,5,6;

然后想到Tushar Roy的课程中,包括计算最小矩阵连乘次数的时候,他所使用的方式似乎都是一个个小区间做。 
在此安利一下这个视频,需翻墙:https://www.youtube.com/watch?v=vgLJZMUfnsU&list=PLrmLmBdmIlpsHaNTPP_jHHDx_os9ItYXr&index=3

回顾一下在矩阵链乘时候他的做法【虽然我和他的推导方式不太一样但是实质都是区间DP】: 
 
对于这道题,我们也可以用类似的方法: 
 
当len=2的时候,可以得到相应的长度为4,5,6; 
同理,len=3的时候 
 
看到这一步,状态的转移就非常明显了,而我们也可以就此继续推导到剩余所有的值。而dp[0][3]即是我们要求的整个木棍的分割产生的价值量。

整理一下思路重新出发: 
对于长度为10,切点为2、4、7的木棍 
可以将其切分为2、2、3、3一共4段 
其DP方程为: 
dp[i][j]=0 if i==j 显然单段木棍是不不需要切分不会产生cost 
             sum[i~j]+min{dp[i][k]+dp[k+1][j]} if i<=k < j  
             即当需要切分的时候,所需要的cost为整段木棍的长度【sum】和其子问题切分所需要的cost。

完美收官。

 

代码

对于DP的方式,我一开始是采取同我所写的矩阵链乘同样的求法,采用逆推的方式 


因为dp方程里面,dp[i][j]所依赖的状态dp[i][k]、dp[k+1][j]必须出现在[i][j]之前,所以要想得出它们必须得倒推才能得出正确的结果; 
 

//遍历长度 从刚开始长度为1,及0~1,1~2,2~3,到长度为2,0~2,2~4,4~6,长度为3,以此类推
for(int len=1;len<=n;len++){
    //长度不可以超过总长 
    for(int i=0;i<n && i+len<n;i++){
        int j=i+len;//结束点
        //需要计算i~j的总量
        int sum=0;
        int min=INT_MAX;
        for(int k=i;k<=j;k++) sum+=woods[k];

        //状态转移
        for(int k=i;k<j;k++){
            if(dp[i][k]+dp[k+1][j]<min)
            min=dp[i][k]+dp[k+1][j];
        }
        dp[i][j]=sum+min;
    }
}

区间动态规划问题一般都是考虑,对于每段区间,他们的最优值都是由几段更小区间的最优值得到,是分治思想的一种应用,将一个区间问题不断划分为更小的区间直至一个元素组成的区间,枚举他们的组合 ,求合并后的最优值。
设F[i,j](1<=i<=j<=n)表示区间[i,j]内的数字相加的最小代价
最小区间F[i,i]=0(一个数字无法合并,∴代价为0)

每次用变量k(i<=k<=j-1)将区间分为[i,k]和[k+1,j]两段

for(p=1;p<=n;p++) // p是区间长度,作为阶段。 
for(i=1;i<=n;i++) // i是穷举的区间的起点
{
j=i+p-1; // j是 区间的终点,这样所有的区间就穷举完毕
if(j>n)  break; // 这个if很关键。
for(k=i;k<j-1;k++)// 状态转移,去推出 f[i,j]
f[i , j]= max{f[ i,k]+ f[k+1,j]+ w[i,j] } 
}
这个结构必须记好,这是区间动态规划的代码结构。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deebcjrb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值