-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10034 - Freckles.cpp
79 lines (61 loc) · 1.23 KB
/
10034 - Freckles.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
#include<bits/stdc++.h>
using namespace std;
class obj{
public :
int u , v;
double prop;
obj(int a , int b, double x){
u = a, v = b;
prop = x ;
}
};
class Compare{
public :
bool operator ()(obj &a , obj &b){
return a.prop > b.prop;
}
};
inline double cal( pair<double , double> &x, pair<double, double > &y){
return sqrt((x.first-y.first)*(x.first-y.first)+(x.second-y.second)
* (x.second-y.second));
}
void solve(){
priority_queue<obj , vector<obj >, Compare > pq;
int n;
scanf("%d",&n);
vector< pair<double, double> > v(n);
for(int i = 0 ; i < n; i++){
double x , y;
scanf("%lf%lf",&x,&y);
v[i] = {x,y} ;
}
for(int i = 1 ; i < n ; i++){
obj temp(0,i,cal(v[0],v[i]));
pq.push(temp);
}
bool isvisited[n+5];
memset(isvisited,false,sizeof isvisited);
isvisited[0] = true;
double sum = 0.0;
while(!pq.empty()){
obj temp = pq.top();
pq.pop();
if(isvisited[temp.v])
continue ;
sum += temp.prop;
isvisited[temp.v] = true;
for(int i = 0 ; i < n; i++)
if(i != temp.v){
obj x(temp.v,i, cal(v[temp.v] , v[i]));
pq.push(x);
}
}
printf("%.2lf\n",sum);
}
int main(){
int t ;
scanf("%d",&t);
while(t--)
solve(),printf("%s",(t?"\n":""));
return 0;
}