HDU 1158 Employment Planning

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BBHHTT/article/details/82021388

Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6215    Accepted Submission(s): 2700

Problem Description

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

思路:

1.dfs+dp
dp[i][j] 表示 第i个月时有j个人时,剩余月份所需开销最小。 则dp[0][0] 即为最后结果。 
对于第i 个月 刚开始有 j  个人,j 如果小于等于上月人数,则需要新招聘人员,那么dp[i][ j ]=dp[i+1][person[i]] +cost[i],如果j大于当月需要人数,则需要解雇,这时需要遍历减几个人是剩余月份开销最小。

2.dp

状态转移方程:dp[i][j]=min(dp[i-1][k]+cost[i][j]);

1.dp[i][j]表示第i个月的j个人所用的最小费用,cost[i][j]是第i月j个人的花费

2.当k<=j时,第i个月请了人,所以cost[i][j]=j*salary+(j-k)*hire;
3.当k<j时,第i个月辞了人所以cost[i][j]=j*salary+(k-j)*fire;

因为给的是每个月最小需要的人数,所以for(该月最小人数---max_people)而不是for(1---该月的最小人数)

//bfs+dp
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int mat[15];
int dp[15][10005];
int n,a,b,c;
#define inf 0x3f3f3f3f
int dfs(int x,int sum) {//月份,人数 
	if(x==n) return 0;
	if(dp[x][sum]!=-1) return dp[x][sum];
	int ans=0;
	if(sum<=mat[x]) {//当前月人数mat[x]比上个月人数sum多,雇人 
		ans=dfs(x+1,mat[x])+a*(mat[x]-sum)+mat[x]*b;
	}
	else {
		ans=inf;
		for(int i=mat[x];i<=sum;i++) {//解雇,看解雇多少人最优 
			ans=min(ans,dfs(x+1,i)+c*(sum-i)+i*b);
		}
	}
	dp[x][sum]=ans;
	return dp[x][sum];
} 
int main()
{
	while(~scanf("%d",&n)&&n!=0) {
		scanf("%d%d%d",&a,&b,&c);
		for(int i=0;i<n;i++) {
			scanf("%d",&mat[i]);
		}
		memset(dp,-1,sizeof(dp));
		printf("%d\n",dfs(0,0));
	}
	return 0;
}
//dp
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int mat[15];
int dp[15][10005];
#define inf 0x3f3f3f3f
int main()
{
	int n,a,b,c;
	while(~scanf("%d",&n)&&n!=0) {
		int maxnum=0;
		scanf("%d%d%d",&a,&b,&c);
		for(int i=1;i<=n;i++) {
			scanf("%d",&mat[i]);
			maxnum=max(maxnum,mat[i]);
		}
		for(int i=mat[1];i<=maxnum;i++) {//初始化 
			dp[1][i]=i*(a+b);
		}
		for(int i=2;i<=n;i++) {//从第二个月开始,只有第一个月初始化了 
			for(int j=mat[i];j<=maxnum;j++) {
				int minn=inf;
				for(int k=mat[i-1];k<=maxnum;k++) {
					if(k>=j) 
						minn=min(dp[i-1][k]+(k-j)*c+j*b,minn);
					else
						minn=min(dp[i-1][k]+(j-k)*a+j*b,minn);
				}
				dp[i][j]=minn;
			}
		}
		int ans=inf;
		for(int i=mat[n];i<=maxnum;i++) {
			ans=min(ans,dp[n][i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/BBHHTT/article/details/82021388