hdu 3506 Monkey Party 区间DP+四边形不等式优化

Monkey Party

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3766    Accepted Submission(s): 1436


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
 
题意:
石子归并问题在一个圆形操场的四周摆放着n 堆石子。现要将石子有次序地合并成一堆。
规定每次只能将相邻的2 堆石子合并成新的一堆,合并的代价为两堆石子块数之和,求合并到一堆的最小代价。
 
因为是环形,要转换成线性的更好处理,即将前n个数依次移到第n个数后面,就拿输入样例数据来说明:
5 2 4 7 6 1 3 9 5 2 4 7 6 1 3 9
 
用dp[i][j]表示将[i,j]合并要付出的代价,用for循环将所有的dp[i][j]值处理出来之后,最后再用一个for循环枚举所有的dp[i][i+n-1]值,输出最小值就是答案
 
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define ll long long
using namespace std;
int n;
ll a[1005],dp[2005][2005],sum[2005],s[2005][2005];
ll min(ll a,ll b)
{
    return a<b?a:b;
}
void init()//初始化
{
    for(int i=1;i<=2*n;i++)
    {
        for(int j=1;j<=n*2;j++)
            dp[i][j]=9999999999;
        s[i][i]=i;
        dp[i][i]=0;
    }
}
int main()
{
    while(cin>>n)
    { 
        init();
        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            sum[i]=sum[i-1]+a[i];//前缀和
        }
        for(int i=1;i<=n;i++)//把环形转化为线性处理
             sum[i+n]=sum[i+n-1]+a[i];

        for(int len=2;len<=n;len++)
        {
            for(int i=1;i+len-1<=2*n;i++)
            {
                int j=i+len-1;
                if(j>n*2)
                    break;
                dp[i][j]=9999999999;
                for(int k=s[i][j-1];k<=s[i+1][j];k++)//四边形不等式优化
                {
                    if(dp[i][j]>dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1])
                    {
                        s[i][j]=k;
                        dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
                    }
                }
            }
        }
        ll ans=9999999999;
        for(int i=1;i<=n;i++)//枚举最优解
        {
            int j=i+n-1;
            ans=min(ans,dp[i][j]);
        }
        cout<<ans<<endl;
    }
    
    return 0;

}
原文地址:https://www.cnblogs.com/-citywall123/p/10902315.html