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)