-
Notifications
You must be signed in to change notification settings - Fork 2
/
10034.cpp
executable file
·130 lines (113 loc) · 1.94 KB
/
10034.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std ;
#define MaxP 100
class point
{
public:
double x ;
double y ;
static double d( point a, point b )
{
return ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) ;
}
bool used ;
};
point p[ MaxP ] ;
int usedp = 0 ;
double find_shortest( int n )
{
int p1 = 0 ;
int p2 = 0 ;
double minLen = -1 ;
double length = 0 ;
int i = 0 ;
int j = 0 ;
for( i=0 ; i<n ; i++ )
{
for( j=0 ; j<n ; j++ )
{
if( i!=j )
{
length = sqrt( (p[i].x-p[j].x)*(p[i].x-p[j].x) +
(p[i].y-p[j].y)*(p[i].y-p[j].y) ) ;
if( minLen < 0 || length < minLen )
{
minLen = length ;
p1 = i ;
p2 = j ;
}
}
}
}
p[ p1 ].used = true ;
p[ p2 ].used = true ;
usedp = 2 ;
return minLen ;
}
double mst( int n )
{
int smallp = 0 ;
int i = 0 ;
int j = 0 ;
double minLen = -1 ;
double length = 0 ;
for( i = 0 ; i < n ; i++ )
{
if( p[ i ].used == true )
{
for( j = 0 ; j < n ; j++ )
{
if( p[j].used == false )
{
length = sqrt( (p[i].x-p[j].x)*(p[i].x-p[j].x) +
(p[i].y-p[j].y)*(p[i].y-p[j].y) ) ;
if( minLen < 0 || length < minLen )
{
minLen = length ;
smallp = j ;
}
}
}
}
}
p[ smallp ].used = 1 ;
usedp++ ;
return minLen ;
}
int main()
{
int testcase = 0 ;
int running = 0 ;
int n = 0 ;
double minLen = 0 ;
int i = 0 ;
cin >> testcase ;
for( running = 0 ; running < testcase ; running++ )
{
cin >> n ;
for( i = 0 ; i < n ; i++ )
{
cin >> p[ i ].x >> p[ i ].y ;
p[ i ].used = false ;
}
if( n < 2 )
{
cout << "0.00" << endl ;
}
minLen = find_shortest( n ) ;
while( usedp < n )
{
minLen += mst( n ) ;
}
// The outputs of two consecutive cases will be separated by a blank line.
if( running > 0 )
{
cout << endl ;
}
cout << setprecision( 2 ) << fixed << minLen << endl ;
}
return 0 ;
}