HDU1158 Employment Planning Basic DP

Source: Internet
Author: User

Employment Planning

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'll be is some extra cost. Once A worker is hired, he'll 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 would confront such a problem:how many workers he'll hire or fire each month in order to keep the Lowes T 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 are 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 Input34 5 690 

Sample Output199the boss hires people to do a project, to do n monthsgive: Recruit fee hire, worker wages Sal, expel person's expense fire. The first few months will require at least a few people. ask for the minimum cost of completing the project. You can hire m individuals for a period of one months, allowing only a few of them to work, but pay m for personal payThe final one months after the completion of the project, this time is not necessary for each worker fire. Dp[i][j] For the first month, the minimum fee required to hire a J personthen the range of J: Num[i]<=j<=max_numinitialization: num[0]=0;Dp[0][j]=j*hire (0<=j<=max_num)Sub-case recursion:1.dp[i-1][k] num[i-1]<=k<=j to hire j-k.2.dp[i-1][k] J<k<max_num fire off k-j personal I just started the wrong place and wrote it in a note.
1#include <iostream>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intinf=0x3f3f3f3f;8 Const intmaxn= -;9 Ten intdp[maxn][10000]; One intNUM[MAXN]; A  - intMain () - { the     intN; -      while(cin>>N) -     { -         if(!N) +              Break; -  +         intFire,sal,hire; A  atCin>>hire>>sal>>Fire ; -  -         //int min_num=inf; //not necessary. -         intmax_num=-inf; -  -          for(intI=1; i<=n;i++) in         { -Cin>>Num[i]; to              +              -             //because with this step if judgment, the             //judgment succeeds, resulting in the following else not executing *             //It's not right to ask for the last Max_num . $             //Max_num to use in the back .Panax Notoginseng             //so it's WA. -             //It also shows that at the same time at the same time seeking Min,max the             //to write a 2 if +             //and not with if else A          the              +             /*if (num[i]<min_num) - Min_num=num[i]; */ $              $             /*Else*/ if(num[i]>max_num) -max_num=Num[i]; -         } the  -dp[0][0]=0;Wuyi  the          for(intj=0; j<=max_num;j++) -dp[0][j]=j*Hire; Wu  -          for(intI=1; i<=n;i++) About              for(intj=0; j<=max_num;j++) $dp[i][j]=inf; -  -          for(intI=1; i<=n;i++) -         { A              for(intj=num[i];j<=max_num;j++) +             { the                  for(intk=num[i-1];k<=j;k++) -                 { $Dp[i][j]=min (dp[i][j],dp[i-1][k]+ (j-k) *hire+j*sal); the                 } the                  for(intk=j+1; k<=max_num;k++) the                 { theDp[i][j]=min (dp[i][j],dp[i-1][k]+ (K-J) *fire+j*sal); -                 } in             } the         } the  About         intans=inf; the  the          for(intj=num[n];j<=max_num;j++) the             if(dp[n][j]<ans) +ans=Dp[n][j]; -  thecout<<ans<<Endl;Bayi  the     } the     return 0; -}
View Code

HDU1158 Employment Planning Basic DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.