Employment Planning (简单DP)

Employment Planning (简单DP)

问题描述:
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.
(项目经理想确定每月需要多少工人。他确实知道每个月所需工人的最低数量。当他雇佣或解雇一名工人时,会有一些额外的成本。一旦一个工人被雇佣,即使他不工作,他也会得到工资。经理知道雇佣工人、解雇工人的成本和工人的工资。然后,经理将面临这样一个问题:为了保持项目的最低总成本,他每个月将雇佣或解雇多少工人。)
输入描述:
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’.
(输入可能包含几个数据集。每个数据集包含三行。第一行包含计划使用的不超过12个月的项目。第二行包含雇佣工人的成本,工资的金额,解雇工人的成本。第三行包含几个数字,表示每个月所需工人的最小数量。输入以包含单个“0”的行结束。)
输出描述:
The output contains one line. The minimal total cost of the project.
样例输入:
3
4 5 6
10 9 11
0
样例输出:
199
AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <iostream>
#define inf 0x3f3f3f
using namespace std;
int n,x,y,z;
int a[20];
int dp[15][2000];
int main()
{
    while(~scanf("%d",&n),n)
    {
        int maxx=-1;
        memset(a,0,sizeof(a));
        scanf("%d%d%d",&x,&y,&z);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            maxx=max(maxx,a[i]);
        }
        memset(dp,inf,sizeof(dp));
        for(int i=a[1]; i<=maxx; i++)
            dp[1][i]=x*i+y*i;
        for(int i=2; i<=n; i++)
            for(int j=a[i]; j<=maxx; j++)
                for(int k=0; k<=maxx; k++)
                {
                    if(k<=j)
                        dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*x+j*y);
                    else
                        dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*z+j*y);
                }
        int num=inf;
        for(int j=0; j<=maxx; j++)
            num=min(num,dp[n][j]);
        printf("%d\n",num);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值