problem DescriptionA 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.
InputThe 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 '.
OutputThe output contains one line. The minimal total cost of the project.
Sample Input
3 4 5 610 9 110
Test instructions: If you are a project manager, when you start a project involving the hiring of workers, the worker is known to pay a fixed salary every month, and at the time of hiring and firing, there is an extra cost to the worker, asking how the arrangement can make a project cost the least. Three rows per group of test data, Only a positive number in the first line indicates the duration of the project (in months), and the second row of three integers represents the cost of employment, wages, and dismissal. Three behavior the minimum total cost of the project for each month.
Analysis: Not much to say, dp!!
State transfer equation: dp[I [j] = min (dp[i-1] [K]) +extr (k,j) +j*pay; (k=a[i-1]~max)
Code:
#include <stdio.h> #define INF 9999999;int A[13],dp[13][100000],in,out,pay; int extra (int a,int b) {if (a>b) return out* (a); return in* (B-A);} int main () {int i,j,k,n;while (scanf ("%d", &n), N) {scanf ("%d%d%d", &in,&pay,&out); for (i=1;i<=n;i++ ) scanf ("%d", &a[i]), int max=0;for (i=1;i<=n;i++) {if (A[i]>max) max=a[i];} for (i=1;i<=n;i++) for (j=a[i];j<=max;j++) {dp[i][j]=999999;if (i==1) Dp[i][j]=extra (0,j) +j*pay;else{for (k=a[ i-1];k<=max;k++) if (Dp[i][j]>dp[i-1][k]+extra (k,j) +j*pay) Dp[i][j]=dp[i-1][k]+extra (k,j) +j*pay;}} int Min=inf;for (i=a[n];i<=max;i++) if (dp[n][i]<min) min=dp[n][i];p rintf ("%d\n", Min);} return 0;}
C Language Dynamic Programming (8) ___ employed workers (HDU 1158)