Dynamic Planning-optimal binary search tree carrying girl drops ...)

Source: Internet
Author: User

 

Import Java. util. arrays; public class besttree {public int N; Public double W [] []; Public double C [] []; Public int R [] []; Public besttree (int n) {c = new double [n + 2] [n + 1]; W = new double [n + 2] [n + 1]; R = new int [n + 2] [n + 1];} public void optionbinarysearch (int n, Double P [], double Q []) {for (INT I = 1; I <= N; I ++) {C [I] [I-1] = 0; W [I] [I-1] = Q [I-1];} public void optionbinarysearch (int n, Double P [], double Q []) {for (INT I = 1; I <= N; I ++) {C [I] [I-1] = 0; W [I] [I-1] = Q [I-1];} for (int K = N; k> = 1; k --) {for (INT I = 1; I <= K; I ++) {Int J = I-K + 5; W [I] [J] = W [I] [J-1] + P [J] + Q [J]; // when I <= J, C [I, j) = W (I, j) + min {C (I, k-1) + C (k + 1, j)} C [I] [J] = W [I] [J] + C [I] [I-1] + C [I + 1] [J]; R [I] [J] = I; for (int m = I + 1; m <= J; m ++) {double T = W [I] [J] + C [I] [s-1] + C [M + 1] [J]; If (float) T <(float) c [I] [J]) {// compare to minimize the value of C [I] [J] C [I] [J] = T; R [I] [J] = m ;}}} public void bestsolution (int r [] [], int I, Int J) {if (I <= J) {system. out. the root of println ("S" + I + "and S" + J + "is:" + "S" + R [I] [J]); bestsolution (r, i, R [I] [J]-1); bestsolution (R, R [I] [J] + 1, J );}} public static void main (string [] ARGs) {Double P [] = {0, 0.15, 0.1, 0.05, 0.1}; double Q [] = {0.2, 0.05, 0.05, 0.05, 0.05, 0.1}; besttree tree = new besttree (5); tree. n = 5; tree. optionbinarysearch (5, p, q); For (INT I = 1; I <= 5; I ++) {system. out. println (arrays. tostring (tree. R [I]);} tree. bestsolution (tree. r, 1, 5 );}}

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.