码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 10003 - Cutting Sticks

时间:2014-11-05 21:29:46      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   for   sp   strong   

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int a[100];
int n;
int dp[1010][1010];
int dfs(int s,int e)
{
	int i;
	if(dp[s][e]>-1)
		return dp[s][e];
	i=upper_bound(a,a+n,s)-a;
	if(i==n||a[i]>=e)
		dp[s][e]=0;
	for(;i<n&&a[i]<e;i++)
		if(dp[s][e]!=-1)
			dp[s][e]=min(dfs(s,a[i])+dfs(a[i],e)+e-s,dp[s][e]);
		else
			dp[s][e]=dfs(s,a[i])+dfs(a[i],e)+e-s;
	return dp[s][e];
}
int main()
{
	int i,l;
	while(cin>>l&&l)
	{
		cin>>n;
		for(i=0;i<n;i++)
			cin>>a[i];
		memset(dp,-1,sizeof(dp));
		printf("The minimum cutting is %d.\n",dfs(0,l));
	}
}

 Cutting Sticks 

You have to cut a wood stick into pieces. The most affordable company, TheAnalog Cutting Machinery, Inc. (ACM), charges money according to the length ofthe stick being cut. Their procedure of work requires that they only make onecut at a time.

It is easy to notice that different selections in the order ofcutting can led to different prices. For example, consider a stick of length 10meters that has to be cut at 2, 4 and 7 meters from one end. There are severalchoices. One can be cutting first at 2, then at 4, then at 7. This leads to aprice of 10 + 8 + 6 = 24 because the first stick was of 10 meters, theresulting 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 isa better price.

Your boss trusts your computer abilities to find out theminimum cost for cutting a given stick.

Input 

The input will consist of several input cases. The firstline of each test case will contain a positive number l that represents thelength of the stick to be cut. You can assume l < 1000. The next line willcontain the number n (n < 50) of cuts to be made.

The next line consistsof n positive numbers ci (0 < ci < l) representing the places where thecuts 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 theoptimal solution of the cutting problem, that is the minimum cost of cuttingthe 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.



Miguel Revilla
2000-08-21

UVA 10003 - Cutting Sticks

标签:style   blog   io   color   ar   os   for   sp   strong   

原文地址:http://blog.csdn.net/stl112514/article/details/40832277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!