动态规划22 Employment Planning

Employment Planning

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 23

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


#include<iostream>
using namespace std;

int dp[14][1000];//dp[i][j]表示第i个月结束以后剩余j个人的状态下的最小钱数
int hire;
int salary;
int fire;
int mouthval[14];

int main()
{
	int n;
	while (cin >> n, n)
	{
		cin >> hire >> salary >> fire;
			int maxmouthval = -1;
			for (int i = 1; i <= n; ++i)
			{
				cin >> mouthval[i];
				if (maxmouthval < mouthval[i])
					maxmouthval = mouthval[i];
			}
			for (int i = mouthval[1]; i <= maxmouthval; ++i)
				dp[1][i] = i*hire + i*salary;
			for (int i = 2; i <= n; ++i)
			{
				for (int j = mouthval[i]; j <= maxmouthval; ++j)
				{
					int minb = INT_MAX;
					for (int k = mouthval[i - 1]; k <= maxmouthval; ++k)
					{
						int temp;
						if (k <= j)
							temp = hire*(j - k) + dp[i - 1][k];
						else
							temp = fire*(k - j) + dp[i - 1][k];
						if (temp < minb)
							minb = temp;
					}
					dp[i][j] = minb + j*salary;
				}
			}
			int mina=INT_MAX;
			for (int i = mouthval[n]; i <= maxmouthval; ++i)
				if (dp[n][i] < mina)
					mina = dp[n][i];
			cout << mina << endl;
	}
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36921652/article/details/79204597