Problem E: Communication

Source: Internet
Author: User
Problem E: Communication

Time Limit: 1 sec memory limit: 128 MB
Submit: 66 solved: 11
[Submit] [Status] [web board]

Description

Military training has started. specified groups is training all day. But because neu has no playground which is big enough to hold all groups, fresh boys and girls have to train in different positions.

For easier command communication, leader decides to set several communicating lines which connect all the groups. each line connect two groups by straight line. obviusly, It is not need to connect each two groups directly. N groups just need N-1 lines to make all groups connected indirectly.

Your task is, give you n groups 'position, you shocould find N-1 lines that connect all the groups and the total distance of all lines is minimum.

Input

There are several test cases.

In each case, the first line is N, the number of groups. N <= 1000. then n lines, each line has two decimal or integer, which is the ith x-y coordinate. (0 <= X, Y <= 1000 ). there are no two groups in one same position.

Output

Each case output a line, the minimum sum distance of N-1 lines with exactly two digits after a decimal point.

Sample Input
 
30 00 10 230 01 10 1
Sample output
 
2.002.00
 
 
 
 
 
 

This is a graph theory topic. The idea is to start from a point and find the point closest to the minimal spanning tree each time.Accumulating these distances is the result.Note: The train of thought is very simple, but it is easy to time out. Writing a normal train of thought will be a 3rd power of O, and it is easy to time out. To avoid time-out, create an array, it is used to record the distance. First, it starts from the point 0 and records all the distances from all the remaining points to this point in this array. Then, every time a new node is found, check the distance between the remaining unaccessed vertex and the new node. If the distance is smaller than the existing records dis [I] in the array, update the distance, that is to say, the records in the DIS array are always the smallest distance from the remaining point to the smallest spanning tree, therefore, when looking for the next vertex, we only need to perform a loop. Every time we find the smallest value in this array, we will find the next node, and so on.

 
 
 
 
 
 

# Include <iostream>

# Include <math. h>

# Include <stdio. h>

Using Namespace STD;

Typedef Struct

{

Double X, Y;

Bool Value;

} Point;

Double Getdistance (point a, point B)

{

Return POW(POW(A. x-b.x, 2) +POW(A. y-b.y, 2), 0.5 );

}

Point P [1005];

Int Main ()

{

Int T, Count, index;

Double D, D1, sum, length [1001] [1001], distan [1005];

While(CIN> T)

{

Count = 1;

For(Int I = 0; I <t; I ++)

{

Cin> P [I]. x> P [I]. Y;

P [I]. value =True;

}

For(Int I = 0; I <t; I ++)

For(Int J = 0; j <t; j ++)

{

Length [I] [J] = getdistance (P [I], p [J]);

If(I = 0) distan [J] = length [I] [J];

}

P [0]. value =False;

Sum = 0;

Index = 0;

While(Count <t)

{

D = 100000000;

For(Int J = 0; j <t; j ++)

{

If(P [J]. value =True& Distan [J] <D)

{

D = distan [J];

Index = J;

// Cout <D <"<index <Endl;

}

}

Sum + = D;// Cout <index <Endl;

P [Index]. value =False;

Count ++;

For(Int J = 0; j <t; j ++)

{

If(P [J]. value =True& Length [Index] [J] <distan [J])

Distan [J] = length [Index] [J];

}

}

Printf("%. 2f \ n", Sum );

}

Return 0;

}

/*************************************** ***********************

Problem: 1115

User: fot

Language: C ++

Result: accepted

Time: 709 MS

Memory: 9056 KB

**************************************** ************************/

 
 

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.