-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10034.cpp
53 lines (47 loc) · 1.12 KB
/
P10034.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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <set>
#include <cmath>
typedef std::pair<double,double> Pt;
typedef std::pair<double,int> WEdge;
typedef std::set<WEdge> set;
double dist(const Pt &a, const Pt &b) {
return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second));
}
int main() {
Pt pts[100];
bool handled[100];
int cases, N;
std::cin >> cases;
for(int cas = 0; cas < cases; ++cas) {
if(cas > 0)
printf("\n");
// Read and setup data:
std::cin >> N;
for(int i = 0; i < N; ++i) {
std::cin >> pts[i].first >> pts[i].second;
handled[i] = false;
}
double sum = 0;
int numHandled = 0;
set s;
s.insert(WEdge(0, 0));
while(numHandled < N) {
WEdge min = *s.begin();
s.erase(min);
if(handled[min.second])
continue;
sum += min.first;
handled[min.second] = true;
++numHandled;
for(int i = 0; i < N; ++i) {
if(handled[i])
continue;
s.insert(WEdge(dist(pts[i],pts[min.second]), i));
}
} // while(numHandled < N)
printf("%.2f\n", sum);
} // for case
return 0;
}