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)