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 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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[20][100000];

int main()
{
    int h,s,f,n,a[20],maxn,i,j,l;
    while(~scanf("%d",&n),n)
    {
        maxn = 0;
        scanf("%d%d%d",&h,&s,&f);
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            maxn = max(maxn,a[i]);
        }
        for(l = a[0]; l<=maxn; l++)
            dp[0][l] = l*(h+s);
        for(i = 1; i<n; i++)
        {
            for(l = a[i]; l<=maxn; l++)
            {
                if(l>a[i-1]) dp[i][l] = dp[i-1][a[i-1]]+l*s+(l-a[i-1])*h;
                else dp[i][l] = dp[i-1][a[i-1]]+l*s+(a[i-1]-l)*f;
                for(j = a[i-1]+1; j<=maxn; j++)
                {
                    if(j>l) dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(j-l)*f);
                    else dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(l-j)*h);
                }
            }
        }
        int ans = 1<<30;
        for(l = a[n-1]; l<=maxn; l++)
            ans = min(ans,dp[n-1][l]);
        printf("%d\n",ans);
    }

    return 0;
}

HDU1158:Employment Planning(DP)

时间: 2024-10-28 06:24:25

HDU1158:Employment Planning(DP)的相关文章

HDU1158:Employment Planning(线性dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1158 这题又是看了题解,题意是一项工作需要n个月完成,雇佣一个人需要m1的钱,一个人的月工资为sa,辞退一个人需要花费m2的钱,然后给出n的数字, 代表每月需要的最少员工,问如何进行人员调整使最终花费的钱最少. 我已开始就推错了状态转移方程,因为给出的是最少的员工,所以要从当前最少员工~最多员工枚举. 还有每次需要比较num[i-1]与num[i]的大小,具体实现请看代码. #include <iost

Hdu 1158 Employment Planning(DP)

Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1158 一道dp题,或许是我对dp的理解的还不够,看了题解才做出来,要加油了. 只能先上代码了. #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 100 + 28; int dp[15][MAXN]; int workers[

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 ext

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

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]&

Employment Planning

Employment Planning 有n个月,每个月有一个最小需要的工人数量\(a_i\),雇佣一个工人的费用为\(h\),开除一个工人的费用为\(f\),薪水为\(s\),询问满足这n个月正常工作的最小费用,\(n\leq 12\). 解 显然可以猜一个结论,因为工人数不确定,猜测每一个月的工人数量必然为某一个月的工人的最小数量,于是可以设\(f[i][j]\)表示前i个月,拥有工人数量\(b_j\)的最小费用,其中\(b\)为\(a\)的离散化数组,因此有 \(f[i][j]=min(m