Interval type dynamic regulation--stone merging (Pascal)

Source: Internet
Author: User

Title Description DescriptionThere are n heap of stones in a row, each pile of stones have a weight of w[i], each merge can merge adjacent two piles of stones, the cost of a merger is the weight of two piles of stone and w[i]+w[i+1]. Ask what sort of merger sequence it takes to minimize the total merger cost. Enter a description input DescriptionFirst line an integer n (n<=100) second row n integer w1,w2...wn (wi <= 100) outputs description output DescriptionAn integer representing the minimum merger cost this is a classic interval type of motion, if using FMIN[I,J] records to merge the minimum value of J heap from I start, using sum[i,j] record from I start J heap of gravel weight, then the conversion equation is as follows:if (F[i,k]+f[k+i,j]+sum[i,j]) >fmin[i,j]Fmin[i,j]:=f[i,k]+f[k+i,j]+sum[i,j];From 1 to J-1 enumeration K thus finding the combined minimum of J heap stones starting from I; The code is as follows:
varI,j,k,n,min:longint; Sum,fmin:Array[-Ten.. $,-Ten.. $] ofLongint; Num:Array[-Ten.. $] ofLongint; beginREADLN (n);  fori:=1  toN Do    beginread (num[i]); Sum[i,1]:=Num[i]; Fmin[i,1]:=0; End;  forj:=2  toN Do     fori:=1  ton-j+1  DoSum[i,j]:=num[i]+sum[i+1, J-1];  forj:=2  toN Do     fori:=1  ton-j+1  Do      beginFmin[i,j]:=Maxlongint;  fork:=1  toJ-1  Do          if(Fmin[i,k]+fmin[k+i,j-k]+sum[i,j]) >fmin[i,j] ThenFmin[i,j]:=fmin[i,k]+fmin[k+i,j-k]+Sum[i,j]; End; Min:=fmin[1, n]; Writeln (min);End.

Interval type dynamic regulation--stone merging (Pascal)

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.