Employment Planning HDU-1158 (Dynamic Planning)

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
Topic: There are n months in total, knowing the minimum number of employees required each month. Know how much money to hire an employee, fire an employee, an employee's monthly salary. Ask how to arrange the number of employees every month so that the least money is spent. Output money.
Idea: We let dp [i] [j] represent the minimum value of the cost of hiring i employees in month j. It must be transferred from the number of people hired in the previous month. Based on the number of people hired in the previous month, we calculate the minimum cost of hiring j individuals this month.
code show as below:

#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;
}

Come on a hard, ( o ) / ~

Published 652 original articles · won 101 · views 50,000+

Guess you like

Origin blog.csdn.net/starlet_kiss/article/details/105448958