hdu4283

http://acm.hdu.edu.cn/showproblem.php?pid=4283

dp[i][j]表示i到j区间最小

dp[i][j]=min(dp[i+1][k]+dp[k+1][j]+(k-i)*a[i]+(sum[j]-sum[k])*(k-i+1));  i<=k<=j;

#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 99999999
using namespace std;
int a[105];
int sum[105];
int dp[105][105];
inline int min(int a,int b)
{
	return a<b?a:b;
}
int main()
{
	int t;
	scanf("%d",&t);
	int l;
	for(l=1;l<=t;l++)
	{
		memset(a,0,sizeof(a));
		memset(dp,0,sizeof(dp));
		memset(sum,0,sizeof(sum));
		int n;
		scanf("%d",&n);
		int i;
		sum[0]=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum[i]=sum[i-1]+a[i];
		}
		int j,k;
		for(i=1;i<=n;i++)
			for(j=i+1;j<=n;j++)
				dp[i][j]=inf;
		for(int li=1;li<=n-1;li++)
		{
			for(i=1;i<=n-li;i++)
			{
				int j=i+li;
				for(k=i;k<=j;k++)
				{
					dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]+(k-i)*a[i]+(sum[j]-sum[k])*(k-i+1));
				}
			}
		}
		printf("Case #%d: %d\n",l,dp[1][n]);
	}
}


你可能感兴趣的:(hdu4283)