HDU_1158_Employment Planning_dp

Employment Planning

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


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
 
之前做过一次,这次算复习,还是花了不少时间。
dp[i][j]表示第i个月留j个人的最优解。每个月保留mon[i]—maxn的解用于下一次更新。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 999999999
int mon[15];
int dp[15][10000];
int main()
{
    int n,hire,fire,sala;
    while(scanf("%d",&n)!=EOF&&n)
    {
        //memset(dp,0,sizeof(dp));
        scanf("%d%d%d",&hire,&sala,&fire);
        int maxn=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&mon[i]);
            if(maxn<mon[i])
                maxn=mon[i];
        }
        for(int i=1; i<=12; i++)
            for(int j=0; j<=10000; j++)
                dp[i][j]=INF;
        for(int i=1; i<=n; i++)
            for(int j=mon[i]; j<=maxn; j++)
                if(i==1)
                    dp[i][j]=j*(sala+hire);
                else
                {
                    for(int k=mon[i-1]; k<=maxn; k++)
                        if(j>=k)
                            dp[i][j]=min(dp[i][j],dp[i-1][k]+(j-k)*(sala+hire)+k*sala);
                        else
                            dp[i][j]=min(dp[i][j],dp[i-1][k]+(k-j)*fire+j*sala);
                }
        //cout<<dp[1][10]<<'*'<<dp[1][11]<<endl;
       // cout<<dp[2][9]<<'*'<<dp[2][10]<<'*'<<dp[2][11]<<endl;
       // cout<<dp[3][11]<<endl;
        int ans=INF;
        for(int i=mon[n]; i<=maxn; i++)
            if(ans>dp[n][i])
                ans=dp[n][i];
        printf("%d\n",ans);
    }

    return 0;
}
View Code

 

posted on 2016-05-13 21:41  JASONlee3  阅读(268)  评论(0编辑  收藏  举报

导航