HDU1158 Employment Planning 基础DP

              Employment Planning

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

老板雇人做一个工程,要做n个月

给出:招人费hire,工人工资sal,开除人的费用fire。

还有第几个月最少需要几个人。

问完成这个工程最少的花费。

你可以在某一个月雇佣m个人,可以只让其中的部分人工作,但是需要支付m个人的工资

最后一个月做完后,工程完成,此时是不需要给每个工人fire的。

dp[i][j] 表示第i个月,雇佣j个人所需的最少费用

则j的范围:num[i]<=j<=max_num

初始化:num[0]=0;

    dp[0][j]=j*hire  (0<=j<=max_num)

分情况递推:

1.dp[i-1][k]   num[i-1]<=k<=j   还要雇佣j-k人

2.dp[i-1][k]   j<k<max_num     fire掉k-j个人

这道题我刚开始写错了一个地方,写在注释了

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4
 5 using namespace std;
 6
 7 const int inf=0x3f3f3f3f;
 8 const int maxn=14;
 9
10 int dp[maxn][10000];
11 int num[maxn];
12
13 int main()
14 {
15     int n;
16     while(cin>>n)
17     {
18         if(!n)
19             break;
20
21         int fire,sal,hire;
22
23         cin>>hire>>sal>>fire;
24
25         //int min_num=inf;       //不必要的
26         int max_num=-inf;
27
28         for(int i=1;i<=n;i++)
29         {
30             cin>>num[i];
31
32
33             //因为有了这一步if的判断,
34             //判断成功的话,导致下面的else没有执行
35             //这样最后求的max_num是不对的
36             //max_num在后面要用到
37             //所以就wa了
38             //这也说明了,以后同时求min,max的时候
39             //要写成2个if
40             //而不能用 if else
41
42
43             /*if(num[i]<min_num)
44                 min_num=num[i];  */
45
46             /*else*/ if(num[i]>max_num)
47                 max_num=num[i];
48         }
49
50         dp[0][0]=0;
51
52         for(int j=0;j<=max_num;j++)
53             dp[0][j]=j*hire;
54
55         for(int i=1;i<=n;i++)
56             for(int j=0;j<=max_num;j++)
57             dp[i][j]=inf;
58
59         for(int i=1;i<=n;i++)
60         {
61             for(int j=num[i];j<=max_num;j++)
62             {
63                 for(int k=num[i-1];k<=j;k++)
64                 {
65                     dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*hire+j*sal);
66                 }
67                 for(int k=j+1;k<=max_num;k++)
68                 {
69                     dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*fire+j*sal);
70                 }
71             }
72         }
73
74         int ans=inf;
75
76         for(int j=num[n];j<=max_num;j++)
77             if(dp[n][j]<ans)
78                 ans=dp[n][j];
79
80         cout<<ans<<endl;
81
82     }
83     return 0;
84 }

时间: 2024-10-23 04:18:45

HDU1158 Employment Planning 基础DP的相关文章

hdu1158 Employment Planning(dp)

题目传送门 Employment Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6242    Accepted Submission(s): 2710 Problem Description A project manager wants to determine the number of the workers

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 work

HDU 1158 Employment Planning【DP】

题意:给出n个月,雇佣一个人所需的钱hire,一个人工作一个月所需要的钱salary,解雇一个人所需要的钱fire,再给出这n个月每月1至少有num[i]个人完成工作,问完成整个工作所花费的最少的钱是多少. 用dp[i][j]表示在第i个月雇佣j个人所需要的最少花费 先考虑只解雇人和聘请人的情况 1 for(j=num[i];j<=sum;j++) 2 { 3 if(j>num[i-1])//说明雇佣了人 4 dp[i][j]=dp[i-1][num[i-1]]+j*salary+(j-num

hdu 1158 Employment Planning(DP)

题意: 有一个工程需要N个月才能完成.(n<=12) 给出雇佣一个工人的费用.每个工人每个月的工资.解雇一个工人的费用. 然后给出N个月所需的最少工人人数. 问完成这个项目最少需要花多少钱. 思路: 将(i,num):[第i个月拥有num个工人 ]看成一个状态.那么就想到用DP. 看代码 代码: struct node{ int minNum, maxNum; } month[15]; int n; int hire,salary,fire; int dp[15][505]; int main(

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 work

Employment Planning[HDU1158]

Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5292 Accepted Submission(s): 2262 Problem DescriptionA project manager wants to determine the number of the workers needed in eve

zoj 1454 Employment Planning

[题解]: [代码]: 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #define LL long long 7 using namespace std; 8 LL dp[15][100005]; 9 LL N,h,uh,sal,num[15]; 10 LL max(L

HDU 1158 Employment Planning

题意:有一公司要工作n个月每个月需要至少p[i]个人工作,每个人的工资固定位m, 每雇佣一个人要花费h,每炒掉一个人要花费f.求完成n个月工作,公司最小的花费. 思路: Dp[i][j]为前i个月的留j个人的最优解;p[i]<=j<=Max{p[i]}; j>Max{p[i]}之后无意义,无谓的浪费 记Max_n=Max{p[i]}; Dp[i-1]中的每一项都可能影响到Dp[i],即使p[i-1]<<p[i] 所以利用Dp[i-1]中的所有项去求Dp[i]; 对于p[i]&

HDU1158_Employment Planning【DP】

Employment Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3814    Accepted Submission(s): 1574 Problem Description A project manager wants to determine the number of the workers neede