【DP】HDU-1158 Employment Planning

来源:互联网 发布:农村淘宝电话号码多少 编辑:程序博客网 时间:2024/05/17 00:05

Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
 
————————————————————————————————————————————————————————
思路:一开始轻松地想到一个状态转移方程,而且意识到可以使用滚动数组来做。
后来发现是错的。。。我没有认准DP的范围,或者说DP的区间。
首先每个月需要雇佣的人数是有下限无上限的!
其次如果某个月雇佣的人数超过了所有月份的最大雇佣人数,那么这些多出来的人纯粹就是吃白饭的。
这样就有了每个月的下限和上限。
DP就在这个范围里进行。
状态转移方程:
dp[i][j] = dp[i-1][k] + sa*j + ???
如果比上个月人多,就加上雇佣费。否则加上裁员费。
第一维i可以滚动。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define For(i,x,y) for(int i = x; i < y; i++)#define Mem(f,x) memset(f, x, sizeof(f))#define Sca(x) scanf("%d", &x)#define Pri(x) printf("%d\n", x)#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/int n, m, hi, sa, f;int work[15], dp[111];//dp[i][j] = dp[i-1][k] + sa*j + ???int main(){#ifdef J_Surefreopen("000.in", "r", stdin);freopen("999.out", "w", stdout);#endifwhile(Sca(n), n) {Sca(hi), Sca(sa), Sca(f);m = 0;For(i, 0, n) {Sca(work[i]);m = max(m, work[i]);}For(j, work[0], m+1) {dp[j] = (hi+sa) * j;}For(i, 1, n) {For(j, work[i], m+1) {int ret = INF;For(k, work[i-1], m+1) {if(k > j) {ret = min(ret, dp[k]+f*(k-j)+sa*j);}else {ret = min(ret, dp[k]+hi*(j-k)+sa*j);}}dp[j] = ret;}}int ans = INF;For(j, work[n-1], m+1) {ans = min(ans, dp[j]);}printf("%d\n", ans);}return 0;}


0 0