首页 > 代码库 > HDU1158:Employment Planning(DP)
HDU1158:Employment Planning(DP)
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#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int dp[20][100000]; int main() { int h,s,f,n,a[20],maxn,i,j,l; while(~scanf("%d",&n),n) { maxn = 0; scanf("%d%d%d",&h,&s,&f); for(i = 0; i<n; i++) { scanf("%d",&a[i]); maxn = max(maxn,a[i]); } for(l = a[0]; l<=maxn; l++) dp[0][l] = l*(h+s); for(i = 1; i<n; i++) { for(l = a[i]; l<=maxn; l++) { if(l>a[i-1]) dp[i][l] = dp[i-1][a[i-1]]+l*s+(l-a[i-1])*h; else dp[i][l] = dp[i-1][a[i-1]]+l*s+(a[i-1]-l)*f; for(j = a[i-1]+1; j<=maxn; j++) { if(j>l) dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(j-l)*f); else dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(l-j)*h); } } } int ans = 1<<30; for(l = a[n-1]; l<=maxn; l++) ans = min(ans,dp[n-1][l]); printf("%d\n",ans); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。