Triangular division of Convex polygons (Hnoi ' 97)

Source: Internet
Author: User
Tags first row


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.





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.