首页 > 代码库 > hdu 1158(dp)
hdu 1158(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1158
Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3654 Accepted Submission(s): 1502
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)
思路:想得开就是简单dp,想不开就做不出~(1):先明确dp[][]的含义,dp[i][j]表示第i个月,工作人数为j时的最优解;用a[]数组存储每个月的最少的工作人数,用maxn存储n个月最大的工作人数,那么最终的最优解就得从dp[n][a[n]].....dp[n][maxn]找出一个最小值;
(2):明确状态转移方程:dp[i][j]=min(dp[i-1][k]+cost[k]); (a[i-1]<=k<=maxn,cost[k]表示人数从k个人变为j个人的花费);
附上AC代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <cstdio> #include <cmath> const int INF=99999999; using namespace std; int a[15]; int dp[15][1000]; int main() { int n; int hire_cost,work_cost,fire_cost; while(cin>>n&&n) { cin>>hire_cost>>work_cost>>fire_cost; int maxn=0; for(int i=1;i<=n;i++) { cin>>a[i]; if(a[i]>maxn)maxn=a[i]; } for(int i=a[1];i<=maxn;i++)dp[1][i]=(hire_cost+work_cost)*i; for(int i=2;i<=n;i++) for(int j=a[i];j<=maxn;j++) { dp[i][j]=INF; for(int k=a[i-1];k<=maxn;k++) { if(k<j)dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*hire_cost+j*work_cost); else dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*fire_cost+j*work_cost); } } int m=INF; for(int i=a[n];i<=maxn;i++) { if(dp[n][i]<m)m=dp[n][i]; } cout<<m<<endl; } return 0; }
hdu 1158(dp)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。