Employment Planning HDU - 1158(动态规划)

A project manager wants to determine the number of the workers needed in every month. He does know the minimal number of the workers needed in each month. When he hires or fires a worker, there will be some extra cost. Once a worker is hired, he will get the salary even if he is not working. The manager knows the costs of hiring a worker, firing a worker, and the salary of a worker. Then the manager will confront such a problem: how many workers he will hire or fire each month in order to keep the lowest total cost of the project.
Input
The input may contain several data sets. Each data set contains three lines. First line contains the months of the project planed to use which is no more than 12. The second line contains the cost of hiring a worker, the amount of the salary, the cost of firing a worker. The third line contains several numbers, which represent the minimal number of the workers needed each month. The input is terminated by line containing a single ‘0’.
Output
The output contains one line. The minimal total cost of the project.
Sample Input
3
4 5 6
10 9 11
0
Sample Output
199
题意:一共有n个月,知道每个月最少需要的员工数。知道雇佣一名员工,解雇一名员工,一名员工每个月的工资的钱数。问每个月如何安排员工人数,使得花费的金钱最少。输出钱数。
思路:我们令dp[i][j]代表j月我们雇佣i名员工花费的最小价值。肯定是从上一个月雇佣的人数进行转移。根据上面一个月雇佣的人数,我们计算这个月雇佣j个人的最小花费。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=1e4+100;
int dp[maxx][15];
int wnum[15];
int n,hc,wc,fc;

int main()
{
	while(~scanf("%d",&n),n)
	{
		scanf("%d%d%d",&hc,&wc,&fc);
		int _max=0;
		for(int i=1;i<=n;i++) 
		{
			scanf("%d",&wnum[i]);
			_max=max(_max,wnum[i]);
		}
		for(int i=wnum[1];i<=_max;i++) dp[i][1]=i*(hc+wc);//第一个月的初始化一下。
		for(int i=2;i<=n;i++)
		{
			for(int j=wnum[i];j<=_max;j++)//枚举这个月可能雇佣的人数
			{
				int _min=inf;
				for(int k=wnum[i-1];k<=_max;k++)//枚举上一个月雇佣的人数
				{
					int sum=0;
					if(k>=j) sum=dp[k][i-1]+(k-j)*fc+j*wc;
					else sum=dp[k][i-1]+j*wc+(j-k)*hc;//根据不同的情况计算雇佣j个人所需要的花费
					_min=min(_min,sum);//取最小值
				}
				dp[j][i]=_min;
			}
		}
		int _min=inf;
		for(int i=wnum[n];i<=_max;i++) _min=min(_min,dp[i][n]);
		printf("%d\n",_min);
	}
	return 0;
}

努力加油a啊,(o)/~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值