hdu Employment Planning

来源:互联网 发布:乐视网络大电影合作 编辑:程序博客网 时间:2024/05/17 09:13

题目链接   :点击打开链接

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 610 9 110
 

Sample Output
199

题意:

       经理对每个月都可以炒员工鱿鱼(f) , 招人 (h),和必定对留下的人发薪金(s)

要求:

在N个月过程中使费用最少。


分析:

我们可以看出在第N个月的费用中,与N-1个月甚至前面的所有月份都有关系。如果我们用DP[i][j]  表示第i 个月有J个人的情况进

所需要的费用,那么我们可以得出状态转移方程为;

if(j >= k)
cost = ( h*(j - k) )+s*j + dp[i-1][k];
else
cost = ( f*(k - j) )+s*j + dp[i-1][k];
其中     month[i-1]<=k<=max(month[1].....month[n]);


#include<stdio.h>int dp[13][2000];int main(){int month[13];int i,j,k,n,p1,p2,p3;int max,min;while(scanf("%d",&n)!=EOF,n){scanf("%d%d%d",&p1,&p2,&p3);max=0;for(i=1;i<=n;i++) {scanf("%d",&month[i]);if(month[i]>max) max=month[i];}for(i=month[1];i<=max;i++)dp[1][i]=i*(p1+p2);for(i=2;i<=n;i++)for(j=month[i];j<=max;j++){min=1000000;for(k=month[i-1];k<=max;k++){if(j>=k) dp[i][j]=p1*(j-k) + p2*j + dp[i-1][k];else dp[i][j]=p3*(k-j) + p2*j + dp[i-1][k];if(dp[i][j]<min) min=dp[i][j];}dp[i][j]=min;}min=1000000;for(i=month[n];i<=max;i++)if(dp[n][i]<min) min=dp[n][i];printf("%d\n",min);}return 0;}

一步一个脚印。相信自己。

原创粉丝点击