HDU1158:Employment Planning(线性dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1158

这题又是看了题解,题意是一项工作需要n个月完成,雇佣一个人需要m1的钱,一个人的月工资为sa,辞退一个人需要花费m2的钱,然后给出n的数字,

代表每月需要的最少员工,问如何进行人员调整使最终花费的钱最少。

我已开始就推错了状态转移方程,因为给出的是最少的员工,所以要从当前最少员工~最多员工枚举。

还有每次需要比较num[i-1]与num[i]的大小,具体实现请看代码。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int n,dp[13][110];
int m1,sa,m2,num[13],maxx;
int main()
{
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        scanf("%d%d%d",&m1,&sa,&m2);
        maxx=-inf;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&num[i]);
            maxx=max(maxx,num[i]);
        }
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=maxx;j++)
                dp[i][j]=inf;
        }
        for(int i=1;i<=maxx;i++)
        {
            dp[1][i]=i*m1+i*sa;
            //printf("dp[1][%d]==%d\n",i,dp[1][i]);
        }
        for(int i=2;i<=n;i++)
        {
            for(int j=num[i];j<=maxx;j++)
            {
                for(int k=num[i-1];k<=maxx;k++)
                {
                    if(k>=j)
                    dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*m2+j*sa);
                    else
                    dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*m1+j*sa);
                }
                 //printf("dp[%d][%d]==%d\n",i,j,dp[i][j]);
            }

        }
       int minx=inf;
       for(int i=num[n];i<=maxx;i++)
        minx=min(minx,dp[n][i]);
       printf("%d\n",minx);
    }
    return 0;
}
时间: 2024-10-31 10:14:46

HDU1158:Employment Planning(线性dp)的相关文章

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

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