Employment Planning
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 6
10 9 11
0
Sample Output
199
老板雇人做一个工程,要做n个月
给出:招人费hire,工人工资sal,开除人的费用fire。
还有第几个月最少需要几个人。
问完成这个工程最少的花费。
你可以在某一个月雇佣m个人,可以只让其中的部分人工作,但是需要支付m个人的工资
最后一个月做完后,工程完成,此时是不需要给每个工人fire的。
dp[i][j] 表示第i个月,雇佣j个人所需的最少费用
则j的范围:num[i]<=j<=max_num
初始化:num[0]=0;
dp[0][j]=j*hire (0<=j<=max_num)
分情况递推:
1.dp[i-1][k] num[i-1]<=k<=j 还要雇佣j-k人
2.dp[i-1][k] j<k<max_num fire掉k-j个人
这道题我刚开始写错了一个地方,写在注释了
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 5 using namespace std; 6 7 const int inf=0x3f3f3f3f; 8 const int maxn=14; 9 10 int dp[maxn][10000]; 11 int num[maxn]; 12 13 int main() 14 { 15 int n; 16 while(cin>>n) 17 { 18 if(!n) 19 break; 20 21 int fire,sal,hire; 22 23 cin>>hire>>sal>>fire; 24 25 //int min_num=inf; //不必要的 26 int max_num=-inf; 27 28 for(int i=1;i<=n;i++) 29 { 30 cin>>num[i]; 31 32 33 //因为有了这一步if的判断, 34 //判断成功的话,导致下面的else没有执行 35 //这样最后求的max_num是不对的 36 //max_num在后面要用到 37 //所以就wa了 38 //这也说明了,以后同时求min,max的时候 39 //要写成2个if 40 //而不能用 if else 41 42 43 /*if(num[i]<min_num) 44 min_num=num[i]; */ 45 46 /*else*/ if(num[i]>max_num) 47 max_num=num[i]; 48 } 49 50 dp[0][0]=0; 51 52 for(int j=0;j<=max_num;j++) 53 dp[0][j]=j*hire; 54 55 for(int i=1;i<=n;i++) 56 for(int j=0;j<=max_num;j++) 57 dp[i][j]=inf; 58 59 for(int i=1;i<=n;i++) 60 { 61 for(int j=num[i];j<=max_num;j++) 62 { 63 for(int k=num[i-1];k<=j;k++) 64 { 65 dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*hire+j*sal); 66 } 67 for(int k=j+1;k<=max_num;k++) 68 { 69 dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*fire+j*sal); 70 } 71 } 72 } 73 74 int ans=inf; 75 76 for(int j=num[n];j<=max_num;j++) 77 if(dp[n][j]<ans) 78 ans=dp[n][j]; 79 80 cout<<ans<<endl; 81 82 } 83 return 0; 84 }