博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Employment Planning DP
阅读量:6302 次
发布时间:2019-06-22

本文共 2964 字,大约阅读时间需要 9 分钟。

Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4782    Accepted Submission(s): 2019

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
 
参考大牛的题解。
dp[i][j]表示前i个月最后一个月的总人数为j所花的最小费用
 
 
状态移动方程:dp[i][j] = min{dp[i-1][k] + cost[i][j]},其中cost[i][j]是第i月的花费,
1~当k<=j时,第i个月请了人所以cost[i][j] = j*salary + (j-k)*hire
2~当k>j时,第i个月炒了人,所以cost[i][j] = j*salary + (k-j)*fire
 
输入时记录了最多需要的人数。
 
因为他给的是每个月最少需要的人数,所以for(该月需要的最少人数——max_people)而不是for(1——该月需要的最少人数)
 
直接初始化第一个月。
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 int dp[15][100]; 8 int people[15]; 9 void solve(){10 int n;11 int hire,salary,fire;12 while(scanf("%d",&n) == 1&&n){13 scanf("%d%d%d",&hire,&salary,&fire);14 int max_people = 0;15 for(int i = 1; i<=n; i++){16 scanf("%d",&people[i]);17 max_people = max(max_people,people[i]);18 }19 // memset(dp,INF,sizeof(dp));20 for(int i = people[1]; i<=max_people; i++){21 dp[1][i] = (hire+salary)*i;22 }23 for(int i = 2; i<=n; i++){24 for(int j = people[i]; j<=max_people; j++){25 int minn = INF;26 for(int k = people[i-1]; k<=max_people; k++){27 int cost = k<=j?(salary*j+(j-k)*hire):(salary*j+(k-j)*fire);28 minn =min(minn,(dp[i-1][k] + cost));29 }30 dp[i][j] = minn;31 }32 }33 int ans = INF;34 for(int j = people[n]; j<=max_people; j++) ans = min(ans,dp[n][j]);35 printf("%d\n",ans);36 }37 }38 int main()39 {40 solve();41 return 0;42 }

 

转载于:https://www.cnblogs.com/littlepear/p/5396074.html

你可能感兴趣的文章
阿里云ECS每天一件事D1:配置SSH
查看>>
SQL Server 性能调优(性能基线)
查看>>
uva 10801 - Lift Hopping(最短路Dijkstra)
查看>>
[Java Web]servlet/filter/listener/interceptor区别与联系
查看>>
POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
查看>>
从零开始学MVC3——创建项目
查看>>
CentOS 7 巨大变动之 firewalld 取代 iptables
查看>>
延时任务和定时任务
查看>>
linux下的权限问题
查看>>
教你如何使用Flutter和原生App混合开发
查看>>
Spring Boot 整合redis
查看>>
CSS hover改变背景图片过渡动画生硬
查看>>
JDBC(三)数据库连接和数据增删改查
查看>>
淘宝应对"双11"的技术架构分析
查看>>
ssh
查看>>
订单的子单表格设置颜色
查看>>
Office365 Exchange Hybrid 番外篇 ADFS后端SQL群集(一)
查看>>
9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路...
查看>>
lvs fullnat部署手册(三)rs内核加载toa篇
查看>>
C++策略模式
查看>>