-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10397.cpp
More file actions
97 lines (93 loc) · 2.28 KB
/
10397.cpp
File metadata and controls
97 lines (93 loc) · 2.28 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf 10000000
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 800
struct edge {
int u, v;
double w;
};
struct node {
double x, y;
};
struct point {
int x, y;
};
node a[NN + 7];
int pr[NN + 7];
vector<edge> e;
vector<point> V;
bool compoint(point n, point m) {
if (n.x == m.x) return n.y < m.y;
return n.x < m.x;
}
bool comp(edge n, edge m) { return n.w < m.w; }
int root(int n) {
if (pr[n] == n) return n;
return root(pr[n]);
}
double mst(int n) {
int i, j, k;
for (i = 0; i <= n; i++) pr[i] = i;
sort(e.begin(), e.end(), comp);
int count = 0;
double sum = 0;
for (i = 0; i < e.size() && count < n - 1; i++) {
int u = root(e[i].u);
int v = root(e[i].v);
if (u != v) {
pr[u] = v;
count++;
sum += e[i].w;
}
}
return sum;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
// ios_base::sync_with_stdio(false);
int i, j, k, l, n, r, c, u, v, w;
edge ed;
node nd;
point pn;
int tc, t = 1, m;
double x;
// scanf("%d",&tc);
while (~scanf("%d", &n)) {
for (i = 1; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y);
scanf("%d", &r);
while (r--) {
scanf("%d%d", &u, &v);
pn.x = min(u, v);
pn.y = max(u, v);
V.push_back(pn);
}
sort(V.begin(), V.end(), compoint);
r = 0;
for (i = 1; i <= n; i++)
for (j = i + 1; j <= n; j++) {
if (r < V.size() && V[r].x == i && V[r].y == j)
x = 0, r++;
else
x = sqrt(((a[i].x - a[j].x) * (a[i].x - a[j].x)) + ((a[i].y - a[j].y) * (a[i].y - a[j].y)));
ed.u = i;
ed.v = j;
ed.w = x;
e.push_back(ed);
}
double sum = mst(n);
printf("%.2lf\n", sum);
e.clear();
V.clear();
}
return 0;
}