首页 > ACM题库 > HDU-杭电 > HDU 3110-Crystal Ball Factory-动态规划-[解题报告]HOJ
2014
03-02

HDU 3110-Crystal Ball Factory-动态规划-[解题报告]HOJ

Crystal Ball Factory

问题描述 :

The Astrologically Clairvoyant Manufacturers (ACM),a pioneer in future-predicting technology, just landed a contract to manufacture crystal balls for weather forecasters around the world. Every week, a variable number of crystal balls needs to be delivered; the required amount for each week is specified in the contract.

Worms

Crystal balls are made from the highest-quality crystal, whose price fluctuates from week to week. Fortunately, the ACM is able to foresee the price of crystal for the coming weeks, thanks to its own future-predicting technology.

When the price is low, the ACM would like to buy crystal and manufacture crystal balls, storing any excess in their warehouse. On the other hand, in weeks for which the price is high, ACM would rather use the crystal balls stored in the warehouse to satisfy the demand specified in their contract. However, since there is a also a fixed weekly cost to store each crystal ball in the warehouse, and an initial cost for turning on the manufacturing machines and producing a non-zero quantity of crystal balls, the decision is not always simple.

Can you help them fulfill their contract at minimal cost?

输入:

The first line of each test case (representing a contract) will contain the number of weeks for which the contract will last.

The next line will contain the non-negative integers b, k and n, where b is the base cost for manufacturing a non-zero quantity of crystal balls on a given week, k is the cost for storing each crystal ball in the warehouse for a week, and n is the maximum capacity of the warehouse.

The following lines will describe the weeks specified in the contract in chronological order. Each week is described by a single line which will contain a pair of non-negative integers c and r, where c is the cost for manufacturing a new crystal ball using new crystal bought this week, and r is the number of crystal balls which must be delivered this week. A crystal ball can be manufactured and delivered in the same week if appropriate, in which case it won’t need to be stored in the warehouse at all.

The last line of the input will contain the integer 0 and should not be processed.

输出:

The first line of each test case (representing a contract) will contain the number of weeks for which the contract will last.

The next line will contain the non-negative integers b, k and n, where b is the base cost for manufacturing a non-zero quantity of crystal balls on a given week, k is the cost for storing each crystal ball in the warehouse for a week, and n is the maximum capacity of the warehouse.

The following lines will describe the weeks specified in the contract in chronological order. Each week is described by a single line which will contain a pair of non-negative integers c and r, where c is the cost for manufacturing a new crystal ball using new crystal bought this week, and r is the number of crystal balls which must be delivered this week. A crystal ball can be manufactured and delivered in the same week if appropriate, in which case it won’t need to be stored in the warehouse at all.

The last line of the input will contain the integer 0 and should not be processed.

样例输入:

4
1 0 1000
1 1
12 4
1 0
1000 1000
2
0 100 1
1 1000
1000 101
0

样例输出:

1007
101101

#include<iostream>
using namespace std;
int dp[1010][1010], price[1010],need[1010];
int main()
{
	int week,basecost,cell,storecost,i,j,k,min,mini;
	while(cin>>week)
	{
		if(week==0)
			break;
		cin>>basecost>>storecost>>cell;
		for(i=1;i<=week;i++)
			cin>>price[i]>>need[i];
		for(j=0;j<=cell;j++)
		{
			dp[1][j]=(j+need[1])*price[1]+basecost+j*storecost;
			if(need[1]==0)
//第一个月不生产				dp[1][0]=0;
		}
		for(i=2;i<=week;i++)
			for(j=0;j<=cell;j++)
			{
				min=10000000;
				for(k=0;k<=cell;k++)
				{
					if(k<=j+need[i])
					{
						if(j==k-need[i])
							mini=dp[i-1][k]+j*storecost;
						else mini=dp[i-1][k]+basecost+(j-k+need[i])*price[i]+j*storecost;
						if(mini<min)
							min=mini;
					}
				}
			    dp[i][j]=min;
			}
		min=dp[week][0];
		for(i=1;i<=cell;i++)
			if(dp[week][i]<min)
				min=dp[week][i];
		cout<<min<<endl;
	}
	return 0
}

F[I][J]表示第i个月存在仓库中j个,他的最小值取决于前一个月剩余的个数,即看要生产多少个,找最小值。

 

 

 

参考:http://blog.csdn.net/killer_in_silence/article/details/8630387


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。