UVA 10003 Cutting Sticks(区间dp)

Description

 Cutting Sticks 

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.

 状态转移方程:d(i,j)=min(d(i,k)+d(k,j)+a[j]-a[i]) (i<k<j)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn=55;
 7 const int INF=10000000;
 8 int d[maxn][maxn],a[maxn],l,n;
 9 inline int min(int a,int b){return a<b?a:b;}
10
11 int dp(int i,int j)
12 {
13     if(i+1==j) return d[i][j]=0;
14     if(d[i][j]!=-1) return d[i][j];
15     d[i][j]=INF;
16     for(int k=i+1;k<j;k++)
17         d[i][j]=min(d[i][j],dp(i,k)+dp(k,j)+a[j]-a[i]);
18     return d[i][j];
19 }
20 int main()
21 {
22     while(~scanf("%d",&l),l)
23     {
24         scanf("%d",&n);
25         memset(d,-1,sizeof(d));
26         for(int i=1;i<=n;i++) scanf("%d",a+i);
27         a[0]=0;a[n+1]=l;
28         printf("The minimum cutting is %d.\n",dp(0,n+1));
29     }
30     return 0;
31 }

UVA 10003 Cutting Sticks(区间dp),布布扣,bubuko.com

时间: 2024-12-26 14:55:35

UVA 10003 Cutting Sticks(区间dp)的相关文章

UVA 10003 Cutting Sticks 区间DP+记忆化搜索

UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的长度L,第二行是切割点的个数n,接下来的n行是切割点在木棍上的坐标. 输出切割木棍的最小费用 前话-区间dp简单入门 区间dp的入门下面博客写的非常好,我就是看的他们博客学会的,入门简单,以后的应用就得靠自己了. https://blog.csdn.net/qq_41661809/article/d

uva 10003 Cutting Sticks (DP)

uva 10003 Cutting Sticks Description 你的任务是替一家叫Analog Cutting Machinery (ACM)的公司切割木棍.切割木棍的成本是根据木棍的长度而定.而且切割木棍的时候每次只切一段.很显然的,不同切割的顺序会有不同的成本.例如:有一根长10公尺的木棍必须在第2.4.7公尺的地方切割.这个时候就有几种选择了.你可以选择先切2公尺的地方,然后切4公尺的地方,最后切7公尺的地方.这样的选择其成本为:10+8+6=24.因为第一次切时木棍长10公尺,

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

uva 10003 Cutting Sticks 【区间dp】

题目:uva 10003 Cutting Sticks 题意:给出一根长度 l 的木棍,要截断从某些点,然后截断的花费是当前木棍的长度,求总的最小花费? 分析:典型的区间dp,其实和石子归并是一样的,花费就是石子的和,那么久不用多说了. AC代码: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include <map> #include <

UVA - 10003 —— Cutting Sticks

很基础的一道区间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); fo

UVA 10003 - Cutting Sticks

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

uva 10003 Cutting Sticks 切木条dp

这道题有些类似矩阵连乘,就是区间的问题.设dp[i][j]表示从i到j的最小花费,那么dp[i][j]=min{dp[i] [k]+dp[k][j]+a[j]-[i]}(i<k<j)其中a[j]-a[i]表示从i到j的长度,即要切这块木条所需的花费.求大区 间得时候小区间已经算出来了,所以符合动态规划的自底向上,而且是最优子结构,这道题我把0和木条长度加到了a 数组里面,就是说一共有n+2个点,每两个相邻的点不用切割,初始化为1 代码: #include<iostream> #in

Uva 10003 Cutting Sticks (类似于最优矩阵连乘的dp)

题意:有一根长度为L的木棍,和n个切割点的位置(按照从小到大排序),你的任务是在这些切割点的位置把棍子切成n+1份,使得总切割费用最小.每次切割的费用等于被切的木棍长度 思路:这道题与最优矩阵连乘的思想一样,那就是分析最优子结构,再根据子结构来定义状态,首先我们假定第一次分割的最优方案是在k处分割,得到0~k与k~L两段木棍,那么如何最优分割0~k与0~L就是它的子问题,我们根据子问题定义状态d(i,j)是分割从割点i到割点j的最优方案,状态转移方程 d(i,j)=min{d(i,k)+d(k,

10003 - Cutting Sticks(DP)

类似于最优矩阵链乘,将长区间划分成段区间求解,换句话说:长区间依赖于段区间 . 因此如果利用二重循环递推的话,枚举的顺序应该是木棍的长度从小到大,因为长区间依赖于短区间的最优解 . 所以动态规划的重点我认为就是对状态的定义和动态规划的方向,  状态的定义要确保覆盖所有状态,规划的方向要遵循一个状态依赖于另一个早已解决的状态.     所以该题有两种解决方法:记忆化搜索和递推 . 我分别用这两种方法AC了,记忆化搜索更简单易想,耗时0.339.但是递推更快!耗时0.086 .细节请见代码: 1.记