提交时间:2024-06-23 09:50:13

运行 ID: 152352

#include<iostream> #include<cstring> using namespace std; int c[52],dp[52][52]; int main(){ while(true){ int l,n; cin>>l; if(l==0){ exit(0); } cin>>n; memset(c,0x3f,sizeof(c)); memset(dp,0x3f,sizeof(dp)); for(int i=1;i<=n;i++){ cin>>c[i]; } c[0]=0; c[n+1]=l; for(int i=0;i<=n+1;i++){ dp[i][i]=0; dp[i][i+1]=0; } for(int i=n;i>=0;i--){ for(int j=i+2;j<=n+1;j++){ for(int k=i+1;k<=j-1;k++){ dp[i][j]=min(dp[i][k]+dp[k][j]+c[j]-c[i],dp[i][j]); } } } cout<<"The minimum cutting is "<<dp[0][n+1]<<"."<<endl; } return 0; }