hdu 3506 monkey party(环形dp)

Problem Description
Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don't know each other, so as the king, SDH must do something. 
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are: 
1.every time, he can only introduce one monkey and one of this monkey's neighbor. 
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows; 
3.each little monkey knows himself; 
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing. 
 

Input
There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.
 

Output
For each case, you should print a line giving the mininal time SDH needs on introducing.
 

Sample Input
  
  
8 5 2 4 7 6 1 3 9
 

Sample Output
  
  
105
 

Author
PerfectCai



感觉心力交瘁,没有精力写题解了,感觉自己的dp实在是太屎了,必须得加强;
昨天看了alpc前辈们的blog,感觉深受鼓舞,加油!


状态转移方程:dp[i][j]=min(dp[i][k]+dp[k+1][j]+sum[i][j]);
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define hehe cout<<"shabi"<<endl

int dp[2010][2010],sum[2010][2010];
int m,ne,s[2010][2010],date[1010];
void dpe()
{
	int temp;
	dp[0][0]=0;
	s[0][0]=0;
	for(int i=1;i<=m;i++)
	{
		for (int j=i;j<=m;j++)
		{
			sum[i][j]=sum[i][j-1]+date[j];
		}
	}
	for (int i=1;i<m;i++)
	{
		dp[i][i]=0;
		s[i][i]=i;
	}
	for (int jg=2;jg<=m-1;jg++)
	{
		for (int i=1;i+jg<=m+1;i++)
		{
			int j=i+jg-1;
			dp[i][j]=1e9;
			for (int k=s[i][j-1];k<=s[i+1][j];k++)
			{

				temp=dp[i][k]+dp[k+1][j]+sum[i][j];
				if(dp[i][j]>=temp)
				{
					s[i][j]=k;
					dp[i][j]=temp;
					//printf("%d %d %d %d %d\n",i,j,k,dp[i][j],sum[i][j]);
				}
			}
		}
	}
}
int main ()
{
	//freopen ("in.txt","r",stdin);
	while (scanf("%d",&ne)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		memset(sum,0,sizeof(sum));
		memset(s,0,sizeof(s));
		for (int i=1;i<=ne;i++) {
			scanf ("%d",&date[i]);
		}
		for (int i=ne+1;i<=2*ne;i++) {
			date[i]=date[i-ne];
		}
		m=2*ne;
		dpe();
		int ans=1e9;
		for (int i=1;i<=ne;i++)
		{
			ans=ans<dp[i][i+ne-1]?ans:dp[i][i+ne-1];
		}
		printf("%d\n",ans);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值