hdu 1158 Employment Planning (DP)

点击打开链接

题目意思:

1,给你一个n代表要工作的总月数

2,给你工人的雇佣工资hire,月工资salary,解雇工资fire

3,给你每个月需要的工人数

求最小费用

dp[i][j]代表第i个月使用j个工人的最小费用

#include <stdio.h>
int man[15];
int dp[15];
#define INF 0x7FFFFFFF
int main()
{
	int month,i;
	int hire,salary,fire,temp;
	while(scanf("%d",&month)==1&&month)
	{
		scanf("%d%d%d",&hire,&salary,&fire);
		for(i=1;i<=month;i++)
			scanf("%d",&man[i]);
		man[0] = 0;
		for(int i=1;i<=month;i++)
		{
			dp[i] = INF;
			temp = 0;
			for(int j=i-1;j>=0;j--)
			{
				if(temp > man[i]) break;
				if(man[j]<temp) continue;
				temp = man[j];
				int newcost;
				if(temp >= man[i])
				{
					newcost = dp[j] + fire * (temp-man[i]) + salary * (i-j) * man[i];
				}
				else
				{
					newcost = dp[j] + hire * (man[i]-temp) + salary * (i-j-1) * temp + salary * man[i];
				}
				if(dp[i] > newcost)
					dp[i] = newcost;
			}
			//printf("dp[%d] = %d\n",i,dp[i]);
		}
		dp[month+1] = INF;
		temp = 0;
		for(int j=month;j>0;j--)
		{
			if(man[j]<temp) continue;
			temp = man[j];
			int newcost = dp[j] + salary * (month-j) * temp;
			if(dp[month+1] > newcost)
				dp[month+1] = newcost;
		}
		printf("%d\n",dp[month+1]);
	}
	return 0;
}


原文地址:https://www.cnblogs.com/yyf573462811/p/6365195.html