UVA 10003 cuting sticks 切木棍 (区间dp)

区间dp,切割dp[i][j]的花费和切法无关(无后效性)

dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了。O(n^3)

四边形不等式尚待学习

#include<bits/stdc++.h> //变量不要取成ignore left之类
using namespace std;
const int maxn = ;
int cut[maxn];
int dp[maxn][maxn];
const int INF = 0x3fffffff;
int main()
{
//freopen("in.txt","r",stdin);
int L,n;
while(scanf("%d",&L),L){
scanf("%d",&n);
cut[] = ; cut[n+] = L;
for(int i = ; i <= n; i++) scanf("%d",cut+i);
for(int i = ; i <= n; i++) dp[i][i+] = ; for(int d = ; d <= n; d++){
for(int i = ,mi = n-d; i <= mi; i++){
int j = i+d+;
dp[i][j] = INF;
for(int k = i+; k < j; k++){
dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j]);
}
dp[i][j] += cut[j]-cut[i];
}
}
printf("The minimum cutting is %d.\n",dp[][n+]);
}
return ;
}
上一篇:SpringBoot+Shiro引起事务失效、错误原因、解决方法


下一篇:RSA原理及生成步骤