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;
}

你可能感兴趣的:(uva 10003 Cutting Sticks)