HDU 1158 – Employment Planning [DP]

Link

传送门

Problem

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.

Mean

有个一老板要雇人,每个月都有一个最小需要雇佣的人数。雇佣一个人需要花A,一个人干活一个月花B,解雇一个人需要花C。问你如何雇佣使得总花费最少,求最少花费。

Analysis

集训队个人赛的题目,这道比较简答的DP竟然没做出来…最近做DP好不爽…

这道题的问题就是没给数据范围,不过后台数据好像并不很大。

dp[i][j]为第i个月雇佣j个人的最小总花费,每个月的雇佣的人数的上限是所有月份的最大值。转移计算一下就好了…

Code

#include <bits/stdc++.h>
using namespace std;

const int inf = 0x3f3f3f3f;
int N, v[15], dp[15][7777];

int main() {
    while(scanf("%d", &N), N) {
        int A, B, C; scanf("%d%d%d", &A, &B, &C);
        int maxv = 0;
        for(int i = 1; i <= N; ++i) {
            scanf("%d", v + i);
            maxv = max(maxv, v[i]);
        }
        for(int i = v[1]; i <= maxv; ++i)
            dp[1][i] = (A + B) * i;
        for(int i = 2; i <= N; ++i) {
            for(int j = v[i]; j <= maxv; ++j) {
                dp[i][j] = inf;
                for(int k = v[i-1]; k <= maxv; ++k)
                    dp[i][j] = min(dp[i][j], dp[i-1][k] + (k > j ? (C * (k-j) + B * j) : (A * (j - k) + B * j)));
            }
        }
        int ans = inf;
        for(int i = v[N]; i <= maxv; ++i)
            ans = min(ans, dp[N][i]);
        printf("%d\n", ans);
    }
    return 0;
}

欢迎留言