题目链接:
https://vjudge.net/problem/UVA-10003
题意:
题解:
dp[i][j]:=切割小木棍i~j的最优费用,枚举中间切割点,这段的费用是该段的长度
d[i][j] = min(d[i][j],dp(i,k)+dp(k,j)+a[j]-a[i]);
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
int len,n;
int a[maxn];
int d[100][100];
int dp(int i,int j){
if(i+1 == j) return 0;
if(d[i][j] != INF) return d[i][j];
for(int k=i+1; k<j; k++){
d[i][j] = min(d[i][j],dp(i,k)+dp(k,j)+a[j]-a[i]);
}
return d[i][j];
}
int main(){
while(cin>>len && len){
cin >> n;
for(int i=1; i<=n; i++)
a[i] = read();
a[0] = 0; a[n+1] = len;
// dp[i][j] = dp[i][k]+dp[k][j];
for(int i=0; i<=n+1; i++)
for(int j=0; j<=n+1; j++)
d[i][j] = INF;
printf("The minimum cutting is %d.\n", dp(0,n+1));
}
return 0;
}