hdu 1158(dp)

来源:互联网 发布:淘宝手机红包在哪设置 编辑:程序博客网 时间:2024/05/22 18:37

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1158

Employment Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3654    Accepted Submission(s): 1502


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 610 9 110
 

Sample Output
199
 

Source
Asia 1997, Shanghai (Mainland China) 
思路:想得开就是简单dp,想不开就做不出~

        (1):先明确dp[][]的含义,dp[i][j]表示第i个月,工作人数为j时的最优解;用a[]数组存储每个月的最少的工作人数,用maxn存储n个月最大的工作人数,那么最终的最优解就得从dp[n][a[n]].....dp[n][maxn]找出一个最小值;

         (2):明确状态转移方程:dp[i][j]=min(dp[i-1][k]+cost[k]);  (a[i-1]<=k<=maxn,cost[k]表示人数从k个人变为j个人的花费);

          附上AC代码:

       

#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cstdio>#include <cmath>const int INF=99999999;using namespace std;int a[15];int dp[15][1000];int main(){        int n;        int hire_cost,work_cost,fire_cost;        while(cin>>n&&n)        {            cin>>hire_cost>>work_cost>>fire_cost;            int maxn=0;            for(int i=1;i<=n;i++)            {               cin>>a[i];               if(a[i]>maxn)maxn=a[i];            }            for(int i=a[1];i<=maxn;i++)dp[1][i]=(hire_cost+work_cost)*i;            for(int i=2;i<=n;i++)             for(int j=a[i];j<=maxn;j++)                {                  dp[i][j]=INF;                  for(int k=a[i-1];k<=maxn;k++)                  {                     if(k<j)dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*hire_cost+j*work_cost);                     else   dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*fire_cost+j*work_cost);                  }                }           int m=INF;           for(int i=a[n];i<=maxn;i++)           {              if(dp[n][i]<m)m=dp[n][i];           }          cout<<m<<endl;        }  return 0;}

0 0
原创粉丝点击