hdu1158 Employment Planning(dp)
Employment Planning
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6242 Accepted Submission(s): 2710
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
Source
Recommend
题意:给出n个月,雇佣价钱,工资和辞退价钱;然后给出每个月所需的人数,
求最小花费
题解:定义dp[i][j]为以i月份结尾,j个人数的花费最小值
则dp[i][j]=min{dp[i-1][k]+cost[i][j]};
其中当k<=j时,cost[i][j]=j*salary+(j-k)*hire;
k>j时,cost[i][j]=j*salary+(k-j)*fire;
边界就是 for(int i=people[1];i<=maxn;i++)
dp[1][i]=i*salary+i*hire;
dp[1][i]=i*salary+i*hire;
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mod 1000000007
#define INF 0x3f3f3f3f
int n;
int people[13];
int dp[13][10000];
int hire,salary,fire;
int main()
{
while(cin>>n)
{
if(n==0) break;
cin>>hire>>salary>>fire;
int maxn=0;
for(int i=1;i<=n;i++)
{
cin>>people[i];
maxn=max(maxn,people[i]);
}
memset(dp,INF,sizeof(dp));
for(int i=people[1];i<=maxn;i++)
dp[1][i]=i*salary+i*hire;
for(int i=2;i<=n;i++)
{
for(int j=people[i];j<=maxn;j++)
{
for(int k=people[i-1];k<=maxn;k++)
{
if(k<=j)
dp[i][j]=min(dp[i][j],dp[i-1][k]+j*salary+(j-k)*hire);
else
dp[i][j]=min(dp[i][j],dp[i-1][k]+j*salary+(k-j)*fire);
}
}
}
int minn=INF;
for(int i=people[n];i<=maxn;i++)
{
minn=min(minn,dp[n][i]);
}
cout<<minn<<endl;
}
return 0;
}