-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path10397.cpp
82 lines (70 loc) · 1.92 KB
/
10397.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
/*
graphs > minimum spanning tree (MST) > minimum spanning subgraph
difficulty: medium
date: 03/Jun/2020
problem: given an implicit complete graph and some edges, compute the cost of the minimum spanning subgraph
by: @brpapa
*/
#include <bits/stdc++.h>
using namespace std;
struct UFDS {
vector<int> p; // p[i] = pai do item i
UFDS(int N) {
p.resize(N);
for (int i = 0; i < N; i++) p[i] = i;
}
int findSet(int i) {
if (p[i] == i) return i;
return p[i] = findSet(p[i]);
}
bool isSameSet(int i, int j) {
return findSet(i) == findSet(j);
}
void unionSet(int i, int j) {
int setI = findSet(i);
int setJ = findSet(j);
if (!isSameSet(setI, setJ)) p[setI] = setJ;
}
};
typedef pair<int, int> ii;
struct Tedge {
int u, v; double w;
Tedge() {}
Tedge(int u, int v, double w) : u(u), v(v), w(w) {}
bool operator<(const Tedge& p) const { return w < p.w; }
};
ii coords[777];
double dist(int u, int v) {
ii cu = coords[u], cv = coords[v];
int dx = abs(cu.first - cv.first);
int dy = abs(cu.second - cv.second);
return sqrt(dx*dx + dy*dy);
}
int main() {
int V;
while (cin >> V) {
for (int v = 0; v < V; v++) {
int x, y; cin >> x >> y; coords[v] = ii(x, y);
}
vector<Tedge> edgeList; // arestas do grafo completo, priorizando as mais leves
for (int u = 0; u < V; u++)
for (int v = u+1; v < V; v++)
edgeList.push_back(Tedge(u, v, dist(u, v)));
sort(edgeList.begin(), edgeList.end());
UFDS uf(V); // conjuntos de componentes conectados
int E; cin >> E;
while (E--) {
int u, v; cin >> u >> v; u--; v--;
uf.unionSet(u, v);
}
double ans = 0;
for (Tedge e : edgeList) {
if (!uf.isSameSet(e.u, e.v)) {
uf.unionSet(e.u, e.v);
ans += e.w;
}
}
printf("%.2f\n", ans);
}
return 0;
}