-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10397.cpp
69 lines (62 loc) · 1.55 KB
/
P10397.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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
typedef std::pair<int,int> PP;
typedef std::pair<long,PP> DPP;
#define X first
#define Y second
long distSq(PP a, PP b) {
return (a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y);
}
unsigned int find(unsigned int i, unsigned int *uf) {
unsigned int store = i;
while(i != uf[i])
i = uf[i];
unsigned int j = store;
while(i != j) {
store = uf[j]; // next.
uf[j] = i;
j = store; // stored next.
}
return i;
}
void _union(unsigned int i, unsigned int j, unsigned int *uf) {
i = find(i, uf);
j = find(j, uf);
if(i == j)
return;
uf[i] = j;
}
int main() {
unsigned int N, M, a, b, uf[750]; // n = number of buildings, m = existing connections.
DPP *dists = new DPP[750*750];
PP buildings[750];
while(std::cin >> N) {
for(unsigned int i = 0; i < N; ++i) {
std::cin >> buildings[i].X >> buildings[i].Y;
uf[i] = i;
}
std::cin >> M;
for(unsigned int i = 0; i < M; ++i) {
std::cin >> a >> b; --a; --b;
_union(a, b, uf);
}
int distsI = 0;
for(unsigned int i = 0; i < N; ++i)
for(unsigned int j = i+1; j < N; ++j)
dists[distsI++] = DPP(distSq(buildings[i], buildings[j]), PP(i,j));
std::sort(dists, dists+distsI);
double ret = 0;
for(int i = 0; i < distsI; ++i) {
a = find(dists[i].second.first, uf);
b = find(dists[i].second.second, uf);
if(a == b)
continue;
_union(a, b, uf);
ret += sqrt(dists[i].first);
}
printf("%.2f\n", ret);
}
return 0;
}