HDU 1158 Employment Planning (简单二维dp)

来源:互联网 发布:淘宝网店装修多少钱 编辑:程序博客网 时间:2024/06/06 03:32

先说一下题意:就是有一个工程要干m月,每个月需要num[i]个工人,每个工人雇佣、工资、解雇,分别需要花费,h、s、f的钱,让你求出这m个月怎么安排,才会使得花费最小。

状态转移方程很好推啊,就是这个月的花费可以有三种情况组成。1.延续上月的,不再雇佣,不在解雇工人。发工资;

2.解雇一部分人,付解雇的钱,再发工资;3.再雇佣一部分人,再发工资;

所以方程式是:if(k > j)   dp[i][j] = min(dp[i][j], dp[i-1][k]+j*s+(k-j)*f); else dp[i][j] = min(dp[i][j], dp[i-1][k]+j*s+(j-k)*h);

i, j表示的是第i个月雇佣j个人。

Employment Planning

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


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

Sample Output
199
 

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000#define LL __int64//#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 2010;using namespace std;int dp[20][maxn];int num[maxn];int main(){    int n;    while(cin >>n)    {        if(!n)            break;        int h, s, f;        int m = -INF;        cin >>h>>s>>f;        for(int i = 1; i <= n; i++)        {            cin >>num[i];            if(num[i] > m)                m = num[i];        }        for(int i = 0; i <= n; i++)            for(int j = 0; j <= m; j++)                dp[i][j] = INF;        dp[0][0] = 0;        for(int i = 1; i <= n; i++)        {            for(int j = num[i]; j <= m; j++)            {                for(int k = num[i-1]; k <= m; k++)                    if(k > j)                        dp[i][j] = min(dp[i][j], dp[i-1][k]+j*s+(k-j)*f);//解雇                    else                        dp[i][j] = min(dp[i][j], dp[i-1][k]+j*s+(j-k)*h);//雇佣            }        }        int _min = 2*INF;        for(int j = 1; j <= m; j++)            if(dp[n][j] < _min)                _min = dp[n][j];        cout<<_min<<endl;    }}


0 0