HDU 1158 [simple dp]

The meaning of the question: Give you a project that takes several months to complete the purchase, and also give you the minimum number of workers required per month. And tell you the amount of hiring, firing each worker, and the monthly salary payable to each worker. Find the minimum cost to complete the project.

This is a simple dp. The idea is to enumerate the number of workers in the previous month and this month, and write a state transition equation.

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#define MAX 6000
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

int s, h, f;
int num[MAX];
int mon;
int dp[MAX][13];
int max(int a, int b) {
    return (a > b) ? a : b;
}

int min(int a, int b) {
    return (a < b) ? a : b;
}
int main(void) {
    int maxx = 0;
    int ans = 0;
    while (~scanf("%d", &mon) && mon) {
        memset(dp, INF, sizeof(dp));
        ans = INF;
        scanf("%d%d%d", &h, &s, &f);
        for (int i = 1; i <= mon; i++) {
            scanf("%d", &num[i]);
            maxx = max(maxx, num[i]);
        }
        if (mon == 1) {
            ans = num[mon] * (h + s);
            printf("%d\n", ans);
        }
        else {
            for (int i = num[1]; i <= maxx; i++) {
                dp[i][1] = i * (h + s);
            }
            for (int i = 2; i <= mon; i++) {
                for (int j = num[i]; j <= maxx; j++) {
                    for (int k = num[i - 1]; k <= maxx; k++) {
                        if (j == k)
                            dp[j][i] = min(dp[j][i - 1] + j * s, dp[j][i]);
                        else if (j > k) {
                            dp[j][i] = min(dp[k][i - 1] + (j - k)*(h + s) + k * s, dp[j][i]);
                        }
                        else {
                            dp[j][i] = min(dp[k][i - 1] + f * (k - j) + s * j, dp[j][i]);
                        }

                    }

                }
            }
            for (int i = num[mon]; i <= maxx; i++) {
                ans = min(ans, dp[i][mon]);
            }
            printf("%d\n", ans);

        }

    }
    return 0;

}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256592&siteId=291194637