Employment PlanningTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 606 Accepted Submission(s): 184 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 Source Asia 1997, Shanghai (Mainland China) Recommend Ignatius 又是一个比较好的DP!WA了很多次!还好是过了! f[i][j]记录第i年有j个员工的最小费用! a[i] 记录的是第i年需要的员工数! 递归式为!f[i][j]=min(f[i-1][k]+其它处理}k:a[i-1]:max; 最后的结果为 min{f[n][j]},j:a[n]::max; my code:: #include<iostream>using namespace std;long a[13];long f[13][1000];int main(){ long c1,c2,c3,i,j,k; long n,max,min; while(cin>>n) { if(n==0) break; cin>>c1>>c2>>c3; max=0; for(i=1;i<=n;i++) { cin>>a[i]; if(a[i]>max) max=a[i]; } for(i=a[1];i<=max;i++) { f[1][i]=(c1+c2)*i; } for(i=2;i<=n;i++) { for(j=a[i];j<=max;j++) { f[i][j]=0x7FFFFFFF; for(k=a[i-1];k<=max;k++) { if(j<k) { if(f[i-1][k]+c3*(k-j)+j*c2<f[i][j]) f[i][j]=f[i-1][k]+c3*(k-j)+j*c2; } else if(j==k) { if(f[i-1][k]+c2*j<f[i][j]) f[i][j]=f[i-1][k]+c2*j; } else if(j>k) { if(f[i-1][k]+c1*(j-k)+c2*j<f[i][j]) f[i][j]=f[i-1][k]+c1*(j-k)+c2*j; } } } } min=f[n][a[n]]; for(i=a[n]+1;i<=max;i++) if(f[n][i]<min) min=f[n][i]; cout<<min<<endl; } return 0; }

评论