您的位置:首页 > 其它

HDU3110 -- Crystal Ball Factory(动态规划)

2015-08-13 21:27 246 查看


Crystal Ball Factory

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 61 Accepted Submission(s): 16



Problem Description

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.



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?

Input

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.

Output

For each test case, output the minimum amount which the ACM will have to spend in order to fulfill the entire contract. All the numbers in the input will be at most 1000.

Sample Input

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


Sample Output

1007
101101


AC代码及题解:

/*
author: tpbluesky
time:   2015年8月13日21:16:21
题解:	动态规划,dp[i][j]表示第i星期存储j个物品的代价,那么dp[i][j] = min(dp[i-1][k]+存储j个物品的代价)
初始时,dp[1][j] = (j+week[i].need)*week[1].price + j*storeprice + base;
有一个坑点,当week[1].need == 0时 ,dp[1][0] = 0;
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#define inf 0x3f3f3f3f
#define eps 1e-8
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long ll;
const int maxn = 1005;
int dp[maxn][maxn];
struct node
{
int price;
int need;
}week[maxn];

int main()
{
int n, base, kstore, cap;
while(scanf("%d",&n) && n)
{
scanf("%d%d%d",&base,&kstore,&cap);
for(int i = 1;i <= n;++i)
scanf("%d%d",&week[i].price,&week[i].need);
for(int i = 0;i <= cap;++i)
{
dp[1][i] = (i+week[1].need)*week[1].price + i*kstore + base;
if(week[1].need == 0)
dp[1][0] = 0;
}
int minx, miny;
for(int i = 2;i <= n;++i)
{
for(int j = 0;j <= cap;++j)
{
minx = inf;
for(int k = 0;k <= cap;++k)
{
if(k - week[i].need <= j)				//大于j的话,这种状态是达不到的,因为已经有j个了
{
if(k - week[i].need == j)			//上星期的减去用掉week[i].need正好是j个,那么久不用制造了
miny = dp[i-1][k] + j*kstore;
else
miny = dp[i-1][k] + (j-k+week[i].need)*week[i].price + j*kstore+base;
minx = min(minx,miny);
}
}
dp[i][j] = minx;
}
}
minx = inf;
for(int i = 0;i <= cap;++i)
minx = min(dp
[i],minx);
printf("%d\n",minx);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: