题目

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

题解

每个月需要一定人数的工人干活,因此需要雇佣一定的人
雇佣新工人需要钱,每个月工资也需要钱,解雇工人也需要钱
根据每个月需要的工人数,合理解雇、雇佣工人,使总钱数最少

按照样例
第一个月 雇佣 10 人 雇佣费用 10*4=40 工资 10*5=50 共计 90
第二个月 不改变人数 工资 10*5=50 共计 50 总计 140
第三个月 雇佣 1 人 雇佣费用 1*4=4 工资 11*5=55 共计 ``59总计199`

dp[i][j] 来记录第 i 个月雇佣 j 个工人需要的最少的钱数
则有 dp[i][j] = min{ dp[i-1][k] }
其中 k 是上一个月可能雇佣的人数(大于等于上个月需要的人数)

又自己把自己绕进去了,做题效率低到爆

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;

const int INF = 0x7FFFFFFF / 2;
const int maxn = 13;
const int maxp = 10000;

int p[maxn];
int dp[maxn][maxp];

int n;
int h,s,f;

int GetMoney(int ss,int vv) {
    if(ss > vv)
        return (ss - vv) * f + vv * s;
    return (vv - ss) * h + vv * s;
}

bool Do() {
    scanf("%d",&n);
    if(n == 0)
        return false;
    scanf("%d%d%d",&h,&s,&f);

    int mp = 0;
    for(int i = 1;i <= n;i++) {
        scanf("%d",&p[i]);
        mp = max(mp,p[i]);
    }
    p[0] = 0;


    memset(dp,-1,sizeof(dp));
    dp[0][0] = 0;

    for(int i = 1;i <= n;i++)
        for(int j = p[i];j <= mp;j++) {
            int Min = INF;
            for(int k = p[i - 1];k <= mp;k++) 
                if(dp[i - 1][k] != -1) 
                    Min = min(Min,dp[i-1][k] + GetMoney(k,j));

            dp[i][j] = Min;
        }

    int ans = INF;
    for(int i = 1;i <= mp;i++)
        if(dp[n][i] != -1)
            ans = min(ans,dp[n][i]);

    printf("%d\n",ans);

    return true;
}

int main() {
    while(Do());
    return 0;
}