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'll be is some extra cost. Once A worker is hired, he'll 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 would confront such a problem:how many workers he'll hire or fire each month in order to keep the Lowes T 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 are 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
Source
Asia 1997, Shanghai (Mainland China)
The boss wants to determine the number of workers needed per month for m months, and the price of a worker is known to be employed,
the worker's monthly salary, lay off the price of a worker and know how many workers he needs at least every month for m months. Q:
How to hire workers, so as to meet the needs of M-month workers at the same time, pay the least money.
Idea: In m months to find the biggest demand max, then no matter how many workers in the next month, the most
It's enough to hire max workers.
State transfer equation: dp[i][j] = min (dp[i-1][k] + cost) (Num[i-1] <=k <= Max)
I hire J person for the first month = Minimum cost when hiring K person last month + employment or dismissal required
the cost such a triple loop traversal results in dismissal. The first tier represents the I month, and the second tier represents the I-month employment of J individuals
, the third tier represents The first i-1 month employs K individuals. The innermost loop draws dp[i][j]. Last when the smallest flower of the first m months
Fee is to Dp[m][i] (Num[m]<=i<=max) to go through the M-month minimum cost.
#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"