UVA 10003 cutting sticks cut wood strips DP

Source: Internet
Author: User

This problem is similar to matrix multiplication, which is the question of interval. Set DP[I][J] represents the minimum cost from I to J, then Dp[i][j]=min{dp[i]

[K]+dp[k][j]+a[j]-[i]} (I<K<J) where a[j]-a[i] represents the length from I to J, which is the cost required to cut this piece of wood. Seeking large areas

The time between the community has been calculated, so in line with the dynamic planning of the bottom-up, but also the optimal substructure, the problem I put 0 and the length of the wood bar to a

Inside the array, that is, there are n+2 points, each two adjacent points without cutting, initialized to 1

Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <vector> #include <set> #include <string> #include <algorithm>using namespace std; int main () {int s,n;int dp[55][55];int a[55];int i,j,r,k,t,m;while (cin >> s,s) {cin >> n;a[1] = 0;a[n+2] = s;for (i=2; i<=n+1; i++) cin >> a[i];m = n+2;for (i=1; i<m; i++) dp[i][i+1] = 0;for (r=2; r<=m-1; r++)//r indicates the length of the interval, with a minimum of 2 , the longest is m-1 {for (i=1; i<=m-r; i++)//i represents the beginning of the interval, the minimum is 1, the longest is based on R to find out {j=i+r;//j indicates the midpoint of the interval dp[i][j] = dp[i+1][j] +dp[i][i+1] + a[j]-a[i ];for (k=i+2; k<j; k++) {t = Dp[i][k] + dp[k][j] + a[j]-a[i];if (T < dp[i][j]) dp[i][j] = t;}}} cout << "The minimum cutting is" << dp[1][m] << '. ' << Endl;} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 10003 cutting sticks cut wood strips DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.