HDU1158:Employment Planning(DP)

2838 단어 dpHDU
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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int dp[20][100000];

int main()
{
    int h,s,f,n,a[20],maxn,i,j,l;
    while(~scanf("%d",&n),n)
    {
        maxn = 0;
        scanf("%d%d%d",&h,&s,&f);
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            maxn = max(maxn,a[i]);
        }
        for(l = a[0]; l<=maxn; l++)
            dp[0][l] = l*(h+s);
        for(i = 1; i<n; i++)
        {
            for(l = a[i]; l<=maxn; l++)
            {
                if(l>a[i-1]) dp[i][l] = dp[i-1][a[i-1]]+l*s+(l-a[i-1])*h;
                else dp[i][l] = dp[i-1][a[i-1]]+l*s+(a[i-1]-l)*f;
                for(j = a[i-1]+1; j<=maxn; j++)
                {
                    if(j>l) dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(j-l)*f);
                    else dp[i][l] = min(dp[i][l],dp[i-1][j]+l*s+(l-j)*h);
                }
            }
        }
        int ans = 1<<30;
        for(l = a[n-1]; l<=maxn; l++)
            ans = min(ans,dp[n-1][l]);
        printf("%d
",ans); } return 0; }

좋은 웹페이지 즐겨찾기