[USACO12MAR]园林绿化Landscaping

题目描述

Farmer John is building a nicely-landscaped garden, and needs to move a large amount of dirt in the process.

The garden consists of a sequence of N flowerbeds (1 <= N <= 100), where flowerbed i initially contains A_i units of dirt.  Farmer John would like to re-landscape the garden so that each flowerbed i instead contains B_i units of dirt.  The A_i's and B_i's are all integers in the range 0..10.

To landscape the garden, Farmer John has several options: he can purchase one unit of dirt and place it in a flowerbed of his choice for X.HecanremoveoneunitofdirtfromaflowerbedofhischoiceandhaveitshippedawayforX.HecanremoveoneunitofdirtfromaflowerbedofhischoiceandhaveitshippedawayforY.  He can also transport one unit of dirt from flowerbed i to flowerbed j at a cost of $Z times |i-j|.  Please compute the minimum total cost for Farmer John to complete his landscaping project.

 

输入

* Line 1: Space-separated integers N, X, Y, and Z (0 <= X, Y, Z <= 1000).
* Lines 2..1+N: Line i+1 contains the space-separated integers A_i and B_i.

 

输出

* Line 1: A single integer giving the minimum cost for Farmer John's landscaping project.

 

样例输入 

4 100 200 11 42 33 24 0

样例输出 

210

题解:这道题目由于数据比较小,所以可以将数据来张开处理。随后,用动态规划解决。

#include <cstdio>
#include <cmath>
int n,x,y,z,la,lb,a[1005],b[1005],f[1005][1005];
using namespace std;
inline int minn(int x,int y)
{
	return x<y?x:y;
}
int main()
{
	scanf("%d%d%d%d",&n,&x,&y,&z);
	for (int i=1;i<=n;i++)
	{
		int xx,yy;
		scanf("%d%d",&xx,&yy);
		for (int j=1;j<=xx;j++) a[++la]=i;//展开初始堆
		for (int j=1;j<=yy;j++) b[++lb]=i;//展开目标堆
	}
	for (int i=1;i<=la;i++) f[i][0]=i*y;//减少i个泥土,要用i*y的费用
	for (int i=1;i<=lb;i++) f[0][i]=i*x;//增加i个泥土,要用i*x的费用
	for (int i=1;i<=la;i++)
	  for (int j=1;j<=lb;j++)
	    f[i][j]=minn(f[i-1][j]+y,minn(f[i][j-1]+x,f[i-1][j-1]+z*abs(a[i]-b[j])));//算最小值
	printf("%d\n",f[la][lb]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouhongkai06/article/details/80615966