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[15];

inline int Min( int a, int b )
{
    return a<b ? a : b;
}

int main()
{
    int N;
    int hire, salary, fire;
    int maxC;
    int i, j, k;
    int minC;
    while( scanf("%d",&N)!=EOF && N )
    {
        scanf("%d%d%d", &hire, &salary, &fire);
        maxC = -1;
        for( i=1;i<=N;i++ )
        {
            scanf("%d",&workers[i]);
            maxC = maxC<workers[i] ? workers[i] : maxC;
        }
        for( i=workers[1];i<=maxC;i++ )
            dp[1][i] = hire*i + salary*i;
        for( i=2;i<=N;i++ )
        {
            for( j=workers[i];j<=maxC;j++ )
            {
                // 前一个月的任何人数都可以引起下一个月部分人数对应的值的改变
                minC = 65552365;
                for( k=workers[i-1];k<=maxC;k++ )
                {
                    if( j>k )
                        minC = Min( minC, dp[i-1][k]+(j-k)*hire+j*salary );
                    else if( k>=j )
                        minC = Min( minC, dp[i-1][k]+(k-j)*fire+j*salary );
                }
                dp[i][j] = minC;
            }
        }
        minC = 65523365;
        for( i=workers[N];i<=maxC;i++ )
            minC = Min( minC, dp[N][i] );
        printf("%d\n", minC);
    }
    return 0;
}
时间: 2024-10-10 02:56:28

Hdu 1158 Employment Planning(DP)的相关文章

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

题意:有一公司要工作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]&

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

【HDOJ】1158 Employment Planning

简单DP. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <climits> 5 #define MAXN 105 6 7 int dp[15][MAXN]; 8 int a[15]; 9 10 int getMin(int a, int b) { 11 return a<b?a:b; 12 } 13 14 int main() { 15 int n,

HDU 1158【简单dp】

题意:给你一个项目,需要几个月来完成买,同时也给你每个月最少需要的工人数.并且告诉你hiring,firing每个工人的钱数,以及每个月应付每个工人的工资.求项目完成时最小花费. 这是个简单dp,思路就是枚举一下上一个月和本月的工人数,写个状态转移方程即可. #include<stdio.h> #include<string.h> #include<queue> #include<iostream> #define MAX 6000 #define INF

hdu 1158(dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1158 Employment Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3654    Accepted Submission(s): 1502 Problem Description A project manage

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