hdu 1158 Employment Planning

Employment Planning

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

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
 
状态:dp[i][j] 前 i 个月,并且第 i 个月留下 j 个人的最小花费 j>=num[i]&&j<=MaxNum
MaxNum=max{num[i]} i>=1&&i<=n
 
转移方程:dp[i][j]=min{dp[i-1][k]+决策代价+j*salary} k>=num[i-1]&&k<=MaxNum
注:上面的工资应该加的是第 j 个月的,因为状态表示是到 前 i  个月为止
k>j 决策代价 (k-j)*fire
k<j 决策代价 (j-k)*hire
 
边界条件及初始化:dp[i][j]=j*(salary+hire) j>=num[i]&&j<=MaxNum&&i>=1&&i<=n
这个初始化是错的,因为其实只有第一个月是可以确定的,其它的都是未知的,你不能直接提前赋值,否则的话,可能在决策枚举进行阶段转移的时候,这个状态是值是不够大的,那么转移到下一个阶段的值不一定是最优的,这个初始化要注意下
 
正确的边界条件和初始化是:dp[1][j]=j*(salary+hire) j>=num[1]&&j<=MaxNum
 1 #include<stdio.h>
 2 #define Min 30000000
 3 #define fmin(a,b) (a<b?a:b)
 4 int dp[13][100];
 5 int main()
 6 {
 7     int n,hire,sa,fire;
 8     int a[13];
 9     while(scanf("%d",&n)!=EOF&&n)
10     {
11         int max=0,i,j,k;
12         scanf("%d%d%d",&hire,&sa,&fire);
13         for(i=1;i<=n;i++)
14         {
15             scanf("%d",&a[i]);
16             if(max<a[i]) max=a[i];
17         }
18         for(i=a[1];i<=max;i++)
19         {
20             dp[1][i]=i*(hire+sa);
21         }
22         for(i=2;i<=n;i++)
23         {
24             for(j=a[i];j<=max;j++)
25             {
26                 int min=Min;
27                 for(k=a[i-1];k<=max;k++)
28                 {
29                     if(k>j) min=fmin(min,dp[i-1][k]-(j-k)*fire+j*sa);//判断得出是解雇人
30                     else min=fmin(min,dp[i-1][k]+(j-k)*hire+j*sa);  //判断得出是雇佣人
31                 }
32                 dp[i][j]=min;
33             }
34         }
35         int t=Min;
36         for(i=a[n];i<=max;i++)
37         {
38             if(t>dp[n][i]) t=dp[n][i];
39         }
40         printf("%d\n",t);
41 
42     }
43     return 0;
44 }

 

 

posted @ 2013-07-29 21:53  ゐ星落★孤晨ね  阅读(176)  评论(0编辑  收藏  举报