首页 > 代码库 > HDU 1158 Employment Planning (DP)
HDU 1158 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
Source
Asia 1997, Shanghai (Mainland China)
雇佣人:有三项费用:雇佣费(h),工资(w),解雇费(f)
给你n个月需要的人数,求最小的支出费用。。。。。
dp[i][j]:第i天雇佣j个人的费用,确定最大的天数maxn
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> using namespace std; int h,w,f; int a[100000]; ;int dp[15][100000]; int main() { int n,maxn,minn; while(~scanf("%d",&n)&&n) { scanf("%d%d%d",&h,&w,&f); int maxn=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]>maxn) maxn=a[i]; } for(int j=a[1];j<=maxn;j++)//第一天的状态可以确定 dp[1][j]=(h+w)*j; for(int i=2;i<=n;i++) { for(int j=a[i];j<=maxn;j++)//j天雇佣的人数 { minn=INT_MAX; for(int k=a[i-1];k<=maxn;k++)//j的前一天雇佣人数 { if(j>=k) dp[i][j]=(j-k)*h+j*w+dp[i-1][k]; else dp[i][j]=(k-j)*f+j*w+dp[i-1][k]; if(dp[i][j]<minn) minn=dp[i][j]; } dp[i][j]=minn;//取最小值 } } minn=INT_MAX; for(int i=a[n];i<=maxn;i++)//不确定最后一天雇佣了几个人,所以要遍历 { if(dp[n][i]<minn) minn=dp[n][i]; } printf("%d\n",minn); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。