码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1158 Employment Planning (DP)

时间:2014-08-14 01:36:07      阅读:332      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   for   ar   

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
 

Source
 
雇佣人:有三项费用:雇佣费(h),工资(w),解雇费(f)
给你n个月需要的人数,求最小的支出费用。。。。。
dp[i][j]:第i天雇佣j个人的费用,确定最大的天数maxn
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
using namespace std;
int h,w,f;
int a[100000];
;int dp[15][100000];
int main()
{
    int n,maxn,minn;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d%d%d",&h,&w,&f);
        int maxn=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>maxn)
                maxn=a[i];
        }
        for(int j=a[1];j<=maxn;j++)//第一天的状态可以确定
            dp[1][j]=(h+w)*j;
        for(int i=2;i<=n;i++)
        {
            for(int j=a[i];j<=maxn;j++)//j天雇佣的人数
            {
                minn=INT_MAX;
                for(int k=a[i-1];k<=maxn;k++)//j的前一天雇佣人数
                {
                    if(j>=k)
                        dp[i][j]=(j-k)*h+j*w+dp[i-1][k];
                    else
                        dp[i][j]=(k-j)*f+j*w+dp[i-1][k];
                    if(dp[i][j]<minn)
                        minn=dp[i][j];
                }
                dp[i][j]=minn;//取最小值
            }
        }
        minn=INT_MAX;
        for(int i=a[n];i<=maxn;i++)//不确定最后一天雇佣了几个人,所以要遍历
        {
            if(dp[n][i]<minn)
                minn=dp[n][i];
        }
        printf("%d\n",minn);
    }
    return 0;
}


HDU 1158 Employment Planning (DP),布布扣,bubuko.com

HDU 1158 Employment Planning (DP)

标签:des   style   http   color   os   io   for   ar   

原文地址:http://blog.csdn.net/u013582254/article/details/38545553

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!