UVA 10003(p278)----CUtting Sticks

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100;
const int INF=1e7;
int d[maxn][maxn];
int l,n,a[maxn];
int solve(int l,int r)
{
    if(d[l][r]<INF) return d[l][r];
    for(int k=l+1; k<=r-1; k++)
    {
        int tmp=solve(l,k)+solve(k,r);
        tmp+=a[r]-a[l];
        if(tmp<d[l][r])
            d[l][r]=tmp;
    }
    return d[l][r];
}
int main()
{
    while(scanf("%d",&l)==1&&l)
    {
        scanf("%d",&n);
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        a[0]=0;
        a[n+1]=l;
        for(int i=0; i<=n+1; i++)
            for(int j=0; j<=n+1; j++)
                d[i][j]=INF;
        for(int i=0; i<=n; i++)
            d[i][i+1]=0;
        int ans=solve(0,n+1);
        printf("The minimum cutting is %d.\n",ans);
    }
    return 0;
}

你可能感兴趣的:(UVA 10003(p278)----CUtting Sticks)