uva 10003 Cutting Sticks

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;

int vis[55][55],d[55][55],a[55];

int dp(int i,int j)
{
    if(i>=j-1) return 0;
    if(vis[i][j]) return d[i][j];
    vis[i][j]=1;
    int ans,k;
    ans=99999999;
    for(k=i+1;k<=j-1;k++)
    ans=min(ans,dp(i,k)+dp(k,j)+a[j]-a[i]);
    d[i][j]=ans;
    return ans;
}
int main()
{
    int i,j,n,l;
    while(~scanf("%d",&l)&&l)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        a[0]=0;
        a[n+1]=l;
        memset(vis,0,sizeof(vis));
        printf("The minimum cutting is %d.\n",dp(0,n+1));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-02-21 12:19  xryz  阅读(85)  评论(0编辑  收藏  举报