Acm hdu 1162 Eddy's picture (simple Minimum Spanning Tree, start to use the template)

Source: Internet
Author: User
Eddy's picture

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 2454 accepted submission (s): 1184

Problem descriptioneddy begins to like painting pictures recently, he is sure of himself to become a painter. every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture. eddy feels very puzzled, in order to change all friends's view to his technical of painting pictures, So Eddy creates a problem for the his friends of you.
Problem descriptions as follows: given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. how many distants does your duty discover the shortest length which the ink draws?

Inputthe first line contains 0 <n <= 100, the number of point. for each point, a line follows; each following line contains two real numbers indicating the (x, y) coordinates of the point.

Input contains multiple test cases. process to the end of file.

Outputyour program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.

Sample input3
1.0 1.0
2.0 2.0
2.0 4.0

Sample output3.41

Authoreddy

Recommendjgshining
 /*  
Hdu1162 Eddy's picture
Given n vertices, find the shortest line segment to connect all vertices. A simple Minimum Spanning Tree
*/
# Include < Stdio. h >
# Include < Math. h >
# Include < String . H >
# Define Maxn 110
Struct Node // Definition point
{
Double X, Y;
} Node [maxn];
Double Cost [maxn] [maxn]; // Consumption Matrix
Double Distance (node A, Node B) // Distance between two points
{
Return SQRT (A. x - B. X) * (A. x - B. X) + (A. Y - B. Y) * (A. Y - B. Y ));
}
// **************************************** *******************************
// Apply a prim Template
// **************************************** *******************************
# Define Typec double
Const Typec INF = 0x3f3f3f ;
Int Vis [maxn];
Typec lowc [maxn];
Typec prim (typec cost [] [maxn], Int N) // Point from 0 ~ N-1
{
Int I, j, P;
Typec minc, res = 0 ;
Memset (VIS, 0 , Sizeof (VIS ));
Vis [ 0 ] = 1 ;
For (I = 1 ; I < N; I ++ ) Lowc [I] = Cost [ 0 ] [I];
For (I = 1 ; I < N; I ++ )
{
Minc = INF;
P =- 1 ;
For (J = 0 ; J < N; j ++ )
If (Vis [J] = 0 && Minc > Lowc [J])
{Minc = Lowc [J]; P = J ;}
If (Minc = INF) Return - 1 ; // Source image not connected
Res + = Minc; vis [p] = 1 ;
For (J = 0 ; J < N; j ++ )
If (Vis [J] = 0 && Lowc [J] > Cost [p] [J])
Lowc [J] = Cost [p] [J];
}
Return Res;
}
// **************************************** ****************************
Int Main ()
{
Int I, j, N;
While (Scanf ( " % D " , & N) ! = EOF)
{
For (I = 0 ; I < N; I ++ )
Scanf ( " % Lf " , & Node [I]. X, & Node [I]. y );
For (I = 0 ; I < N; I ++ )
For (J = 0 ; J < N; j ++ )
{
If (I = J) cost [I] [J] = 0 ;
Else
Cost [I] [J] = Distance (node [I], node [J]);
}
Printf ( " %. 2lf \ n " , Prim (cost, n ));
}
Return 0 ;
}

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.