-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10034 Freckles
79 lines (66 loc) · 1.55 KB
/
10034 Freckles
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;
typedef pair <int , double> pi;
typedef pair<double , int> pd;
typedef vector <pi> vc;
vc vertex[1002];
int arr[1002];
double arr1[1002] , arr2[1002];
priority_queue <pd > pq;
void prims_algo(int src)
{
arr[src] = 1;
for(int i = 0; i < vertex[src].size(); i++)
{
pi v = vertex[src][i];
if(arr[v.first] == -1)
{
pq.push(make_pair(-v.second , -v.first));
}
}
}
int main()
{
ios_base :: sync_with_stdio(0);
cin.tie(0);
int tes;
cin >> tes;
bool f = false;
while(tes--)
{
int n;
cin >> n;
memset(arr,-1 , sizeof arr);
for(int i = 0; i < n; i++)
{
cin >> arr1[i] >> arr2[i];
}
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
double Cost=sqrt((arr1[j] - arr1[i])*( arr1[j] - arr1[i] )+( arr2[j] - arr2[i]) * ( arr2[j] - arr2[i]));
vertex[i].push_back(make_pair(j , Cost));
vertex[j].push_back(make_pair(i , Cost));
}
}
double mst = 0;
prims_algo(0);
while(pq.size())
{
pd front = pq.top();
pq.pop();
int u = - front.second ;
double w = -front.first;
if(arr[u] == -1)
{
mst += w;
prims_algo(u);
}
}
if(f) printf("\n"); f = true;
printf("%.2lf\n", mst);
for(int i = 0; i < n + 1; i++)
vertex[i].clear();
}
}