首页 > 代码库 > HDU1158_Employment Planning【DP】

HDU1158_Employment Planning【DP】

Employment Planning


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

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

4 5 6
10 9 11
0

Sample Output
199
 
Source

Asia 1997, Shanghai (Mainland China)


题目大意:老板想要决定M个月每个月需要的工人数,已知雇佣一个工人的价格,

工人的月薪,解雇一个工人的价格,又知道M个月每个月至少需要多少个工人。问:

怎样雇佣工人,才能使得满足M个月工人需求的同时,付出最少的钱。

思路:在M个月中求出最大的需求人数Max,那么无论下个月需要多少个工人,最多

雇佣Max个工人也就够了。

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

意思是:第i个月雇佣j个人 = 当上个月雇佣k个人时的最小花费 + 雇佣或解雇所需要

的花费这样三重循环遍历得出解雇。第一层代表第i个月,第二层代表第i月雇佣j个人

,第三层代表第i-1月雇佣k个人。最里层循环得出dp[i][j]。最后当第M个月的最小花

费就是将dp[M][i](num[M]<=i<=Max)遍历一遍求得M个月最小花费。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
//dp[i][j] = min(dp[i-1][k] + cost)(num[i-1] <=k <= Max)
int num[15],dp[15][1100],hire,fire,salary;
int main()
{
    int M,Max,Min,temp;
    while(~scanf("%d",&M) && M)
    {
        scanf("%d%d%d",&hire,&salary,&fire);
        Max = 0;
        for(int i = 1; i <= M; i++)
        {
            scanf("%d",&num[i]);
            if(num[i]>Max)
                Max = num[i];
        }

        for(int i = num[1]; i <= Max; i++)
            dp[1][i] = (hire+salary)*i;
        for(int i = 2; i <= M; i++)
        {
            for(int j = num[i]; j <= Max; j++)
            {
                Min = 0xffffff0;
                for(int k = num[i-1];k <= Max; k++)
                {
                    if(k > j)
                        temp = dp[i-1][k] + fire*(k-j) + salary*j;
                    else
                        temp = dp[i-1][k] + hire*(j-k) + salary*j;
                    if(temp < Min)
                        Min = temp;
                }
                dp[i][j] = Min;
            }
        }
        int ans = 0xffffff0;
        for(int i = num[M]; i<= Max; i++)
            ans = min(ans,dp[M][i]);
        printf("%d\n",ans);

    }

    return 0;
}




HDU1158_Employment Planning【DP】