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)/~

发布了652 篇原创文章 · 获赞 101 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/105448958