uva 10003Cutting sticks(动态规划水题)

来源:互联网 发布:怪物猎人x淘宝 编辑:程序博客网 时间:2024/06/07 01:03

题意:给一根木棍,还有n个切割点,每个切割点的费用为当前切割长度,求最小费用。

#include<cstdio>  #include<cstring>  #include<cmath>  #include<cstdlib>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<queue>  #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long  using namespace std;  const int maxn = 50 + 5;const int INF = 0x3f3f3f3f;int d[maxn][maxn], len[maxn];int n, l;int dp(int L, int R) {if(L+1 == R) return 0;if(d[L][R] != -1) return d[L][R];d[L][R] = INF;for(int i = L+1; i < R; i++) d[L][R] = min(d[L][R], dp(L, i)+dp(i, R)+len[R]-len[L]);return d[L][R];}void init() { memset(d, -1, sizeof(d)); cin >> n; for(int i = 1; i <= n; i++) cin >> len[i]; len[0] = 0; len[n+1] = l;}void solve() {printf("The minimum cutting is %d.\n", dp(0, n+1));}int main() {//freopen("input.txt", "r", stdin);while(scanf("%d", &l) == 1 && l) {init();solve();}return 0;}


0 0
原创粉丝点击