First, the question description
Given a convex polygon with n (n<50) vertices (numbered from 1 to n), the weights of each vertex are known. Ask how to divide this convex polygon into N-2 disjoint triangles, making the sum of the weights of the vertices of these triangles a minimum.
Input file: Number of vertices in first row n
Weights for n vertices in the second row (from 1 to N)
Output format: Minimum and value
The way the triangles are composed
Input Example: 5
122 123 245 231
Output example: the minimum is:12214884
The formation of 3 triangle:
3 4 5, 1 5 3, 1 2 3
Second, analysis of test questions
This is a typical dynamic programming problem. Set F[i,j] (I<J) represents the maximum product from vertex I to vertex J convex polygon triangulation, we can get the following dynamic transfer equation:
F[i,j]=min{f[i,k]+f[k,j]+s[i]*s[j]*s[k]} (I<K<J)
Target status: F[1,n]
But we can find that because of the sum of the product here, when the input data is larger than the length of the shape or even the scope of the solid, so we also need high-precision calculation, but this is the basic skills of everyone, the program is not written, please complete the reader by themselves.
III. Reference Procedures
Var s:array[1..50] of Integer;
F:ARRAY[1..50,1..50] of Comp;
D:ARRAY[1..50,1..50] of Byte;
N:integer;
Procedure Init; (Input data)
Var I:integer;
Begin
READLN (N);
For I:=1 to N do Read (S[i]);
End;
Procedure Dynamic; (Dynamic planning)
Var I,j,k:integer;
Begin
For I:=1 to N do
For j:=i+1 to N do f[i,j]:=maxlongint; (Assign initial value)
For i:=n-2 Downto 1 do
For j:=i+2 to N do
For k:=i+1 to J-1 do
If (F[i,j]>f[i,k]+f[k,j]+s[i]*s[j]*s[k]) then
Begin
F[I,J]:=F[I,K]+F[K,J]+S[I]*S[J]*S[K];
D[i,j]:=k; (Record parent node)
End;
End;
Procedure Print (I,j:integer); (Output each triangle)
Begin
If J=i+1 then Exit;
Write (', ', I, ', J, ', d[i,j]);
Out (I,d[i,j]);
Out (D[I,J],J);
End;
Procedure out; (Output information)
Begin
Assign (Output, ' Output.Txt '); Rewrite (Output);
Writeln (' The minimum is: ', f[1,n]:0:0);
Writeln (' The formation of ', N-2, ' triangle: ');
Write (1, "', N, ' d[1,n]);
Out (1,d[1,n]);
Out (D[1,n],n);
Close (Output);
End;
Begin (main program)
Init;
Dynamic;
Out;
End.