Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4846    Accepted Submission(s): 2061

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个人的最优解。每个月保留mon[i]—maxn的解用于下一次更新。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 999999999
int mon[];
int dp[][];
int main()
{
int n,hire,fire,sala;
while(scanf("%d",&n)!=EOF&&n)
{
//memset(dp,0,sizeof(dp));
scanf("%d%d%d",&hire,&sala,&fire);
int maxn=;
for(int i=; i<=n; i++)
{
scanf("%d",&mon[i]);
if(maxn<mon[i])
maxn=mon[i];
}
for(int i=; i<=; i++)
for(int j=; j<=; j++)
dp[i][j]=INF;
for(int i=; i<=n; i++)
for(int j=mon[i]; j<=maxn; j++)
if(i==)
dp[i][j]=j*(sala+hire);
else
{
for(int k=mon[i-]; k<=maxn; k++)
if(j>=k)
dp[i][j]=min(dp[i][j],dp[i-][k]+(j-k)*(sala+hire)+k*sala);
else
dp[i][j]=min(dp[i][j],dp[i-][k]+(k-j)*fire+j*sala);
}
//cout<<dp[1][10]<<'*'<<dp[1][11]<<endl;
// cout<<dp[2][9]<<'*'<<dp[2][10]<<'*'<<dp[2][11]<<endl;
// cout<<dp[3][11]<<endl;
int ans=INF;
for(int i=mon[n]; i<=maxn; i++)
if(ans>dp[n][i])
ans=dp[n][i];
printf("%d\n",ans);
} return ;
}

HDU_1158_Employment Planning_dp的更多相关文章

随机推荐

  1. - > 最大公约数(辗转相除法)和最小公倍数(公式法)

    最大公约数 #include<iostream> using namespace std; int a,b; int gcd(int x,int y){ return x==0?y:gcd ...

  2. Maticsoft Code Generator

    源码:https://github.com/easonjim/MaticsoftCodeGenerator bug提交:https://github.com/easonjim/MaticsoftCod ...

  3. tomcat这种http服务器,是能接收到客户端的断开信息的,并能打印出来

    如,tomcat的运行文件 DEBUG -- CLOSE BY CLIENT STACK TRACE

  4. go语言slice的理解

    Golang slice yongsean  作者 2017.02.17 00:07  打开App 创建切片,len.cap.append b := make([]int, 5) println(le ...

  5. ganglia收集hbase的metrics

    Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于測量数以千计的节点.每台计算机都执行一个收集和发送度量数据(如处理器速度.内存使用量等)的名为 gmond 的守护进程.它将 ...

  6. 每日一支TED——弗兰斯&#183;兰庭:为动物发声的摄影作品——2015年6月3日

    今天是听TED的第11天,从今天開始简单写一下听TED的感受! 刚把得! 弗兰斯·兰庭从一个部落得到一个思想说:全部的动物都是一样的.虽然他们的外形不一样.可是内在确实一样的,他们在外面伪装,可是他们 ...

  7. mac 浏览器解决跨域问题

    Chrome:命令行执行如下命令open -a Google\ Chrome --args --disable-web-security出现如下提示,说明已经开启: Safari: open -a ' ...

  8. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  9. LNMP环境搭建——PHP篇

    一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...

  10. research plan