HDU3506_Monkey Party

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


代码:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxx=2002;

int dp[maxx][maxx],mark[maxx][maxx],sum[maxx][maxx];
//dp[i][j]表示第i个猴子到第j个猴子认识的总代价
//mark[i][j]标示最小分割点处的k值  用于四边形加速
//sum[i][j]i到j花费总和
int data[maxx];

int min(int a,int b){
    return a<b?a:b;
}

int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&data[i]);
            data[i+n]=data[i];
        }
        memset(sum,0,sizeof(sum));
        for (int i=1;i<2*n;i++)
        {
            dp[i][i]=0;
            mark[i][i]=i;
            for (int j=i;j<=n+i;j++)
            {
                sum[i][j]=sum[i][j-1]+data[j];
            }
        }
        for (int st=2;st<=n;st++)
        {
            for (int i=1;i+st<=2*n+1;i++)
            {
                int j=i+st-1;
                dp[i][j]=99999999;
                for (int k=mark[i][j-1];k<=mark[i+1][j];k++)
                {
                    int temp=dp[i][k]+dp[k+1][j]+sum[i][j];
                    if (dp[i][j]>temp)
                    {
                        dp[i][j]=temp;
                        mark[i][j]=k;
                    }
                }
            }
        }
        int ans=9999999;
        for (int i=1;i<=n;i++)
        {
            ans=min(ans,dp[i][i+n-1]);
        }
        printf("%d\n",ans);
    }
    return 0;
}
题意:输入n,表示有n只猴子围成了一个圈来相互认识,然后输入每只猴子要介绍认识另一只猴子所要花费的时间为代价。并且已知猴子A和猴子B互相认识,那么,猴子A所认识的其他猴子 和 猴子B所认识的其他猴子也都能够相互认识。故此时花费的代价就是所有AB猴子认识的猴子所用的时间和。

思路解析:运用动态规划,枚举每个区间的分割点,从而找到最小花费。因为题目中给出的是环,解决的方法就是设一个长度为2*n的数组可以将环转化为线性。

转移方程:dp[i][j]=min(dp[i][k]+dp[k+1][j]+sum[i][j] ,i<k<j)

线性DP处理环问题



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值