Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3005 Accepted Submission(s): 1196
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 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.
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 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'.
OutputThe output contains one line. The minimal total cost of the project.
Sample Input
3 4 5 610 9 110
Sample Output
199
SourceAsia 1997, Shanghai (Mainland China)
RecommendIgnatius
解題思路:本題為動態規劃題目,每個月處理的人數都應該是以本月需要的人數為下限,以這個工期需要人數最多的月份的人數為上限。也許上個月需要的人數很多,本月需要人數很少,下個月需要的人數又很多,這樣就要考慮到招聘費和解僱費。所以,在處理人數時,每個月不是處理一個,而是處理一批。處理時應該是:第i個月聘用j個人(j=emp[i];j<=max1;j++);
狀態轉移方程:pay[i][j]=pay[i][j]<temp?pay[i][j]:temp;其中temp=pay[i-1][k]+j*work+(招聘或解僱所需費用)
#include<stdio.h>#include<algorithm>using namespace std;int main(){ int emp[13]; //儲存每個月需要的人數 int pay[13][1010]; //pay[i][j]儲存第i個月僱傭j個人的最小費用 int max1; //所有工期內需要僱傭的最大人數 int hir,work,fir; //招聘一個人、僱傭一個人、解僱一個人需要花的錢 int temp; int n,i,j,k; while(scanf("%d",&n)&&n) { scanf("%d%d%d",&hir,&work,&fir); max1=0; for(i=1;i<=n;i++) { scanf("%d",&emp[i]); max1=max1>emp[i]?max1:emp[i]; //尋找最大人數 } for(i=0;i<=n;i++) //邊界資料初始化為最小值 pay[i][0]=0; for(i=0;i<=max1;i++) //最開始僱傭的人都需要招聘費 pay[0][i]=i*hir; emp[0]=0; for(i=1;i<=n;i++) { for(j=emp[i];j<=max1;j++) //第i個月聘用j個工人所需的最低花費 { pay[i][j]=0xfffffff; //需處理值初始化化為最大值 for(k=emp[i-1];k<=max1;k++) //對於第i個月聘用j個工人所需的最低花費,檢索上個月所有可能僱傭的人數花費資訊,取最小值 { temp=pay[i-1][k]+j*work; //前面所有的花費加上這個月需要僱傭人的花費 if(k>j) //檢查相對於上個月的人數是招聘了人還是解僱了人,計算費用 temp+=(k-j)*fir; else if(k<j) temp+=(j-k)*hir; pay[i][j]=pay[i][j]<temp?pay[i][j]:temp; //檢索上個月所有可能僱傭的人數花費資訊,取最小值 } } } sort(pay[n]+emp[n],pay[n]+max1+1); //對最後一個月的花費排序,找出最小值 printf("%d\n",pay[n][emp[n]]); } return 0;}