[UVA10003] 切木棍(dp)

**样例:
100
3
25 50 75
10
4
4 5 7 8
0
**

#include
using namespace std;
const int maxn=1010;
int f[maxn][maxn];
int s[maxn];
int main(){
    int l,n;
    while (true){
        cin>>l;
        if (l==0) break;
        cin>>n;
        for (int i=1;i<=n;i++){
            cin>>s[i];
        }
        s[n+1]=l;
        for (int i=0;i<=n+2;i++)
            for (int j=0;j<=n+2;j++)
                f[i][j]=1<<30;
        for (int i=0;i<=n;i++)
            f[i][i+1]=0;            //初始
        for (int l=2;l<=n+1;l++)
            for (int i=0;i<=n+1;i++){
                int j=i+l;
                if (j>n+1) break;      
                for (int k=i+1;k<=j-1;k++)
                    f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
                f[i][j]+=s[j]-s[i];//注意此处
            }
        cout<<"The minimum cutting is "<0][n+1]<<"."<//就dp没什么好讲的

你可能感兴趣的:(dp)