HDU1158:Employment Planning(DP)

來源:互聯網
上載者:User

標籤:hdu   dp   

Problem DescriptionA 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. 
 
InputThe 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‘.
 
OutputThe output contains one line. The minimal total cost of the project.
 
Sample Input
3 4 5 610 9 110
 
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\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.