UVA 10304 Optimal Binary Search Tree (interval dp)

Source: Internet
Author: User

UVA 10304 Optimal Binary Search Tree topic: Give the N nodes (known to each node of the weight) to build, to fulfill the following rules: Zuozi node value is all less than the parent node, the right subtree node value is all greater than the parent node. The minimum total weight of the tree is required for the last build. Total weight = The sum of each node multiplied by the number of layers (starting at 0). Problem Solving Ideas: dp[i][J] The minimum total weight of the tree representing the first node of the interval to the first J-node. d P[J][I]=mIN(d P[J][I],d P[J][k?1]+d P[k+1][I]+sum[I]?sum[J?1] n u Span style= "Font-family:mathjax_math; Font-style:italic, "id=" mathjax-span-634 "class=" Mi ">m [ k "
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;typedef Long LongllintMain () {return 0;}#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#define Nusing namespace STD;typedef Long LongllintNum[n];intDp[n][n], sum[n];intMain () {intN while(scanf("%d", &n) = =1) {sum[0] =0; for(inti =1; I <= N; i++) {scanf("%d", &num[i]); Sum[i] = sum[i-1] + num[i]; }memset(DP,0,sizeof(DP)); for(inti =2; I <= N; i++) { for(intj = i-1; J >0; j--) {Dp[j][i] =0xFFFFFFF; for(intK = J; K <= i; k++) {Dp[j][i] = min (Dp[j][i], dp[j][k-1] + dp[k +1][i] + sum[i]-sum[j-1]-num[k]); }            }        }printf("%d\n", dp[1][n]); }return 0;}

UVa 10304 Optimal Binary Search Tree (interval 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.