-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLAN.cpp
More file actions
82 lines (64 loc) · 1.28 KB
/
Copy pathLAN.cpp
File metadata and controls
82 lines (64 loc) · 1.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
#include <bits/stdc++.h>
using namespace std;
const double INF = 1e10;
int N, M;
pair<int, int> a[501];
double d[501][501];
double getDist(const pair<int, int>& p, const pair<int, int>& q)
{
return sqrt((p.first - q.first) * (p.first - q.first) + (p.second - q.second) * (p.second - q.second));
}
double prim()
{
int V = N;
double ans = 0;
vector<bool> added(V, false);
vector<double> minWeight(V, INF);
vector<int> parent(V, -1);
minWeight[0] = parent[0] = 0;
for(int iter = 0; iter < V; ++iter)
{
int u = -1;
for(int v = 0; v < V; ++v)
if(!added[v] && (u == -1 || minWeight[u] > minWeight[v]))
u = v;
ans += minWeight[u];
added[u] = true;
for(int i=0; i < V; ++i)
{
int v = i;
double weight = d[u][i];
if(!added[v] && minWeight[v] > weight)
{
minWeight[v] = weight;
parent[v] = u;
}
}
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &N, &M);
for(int i=0; i < N; i++)
scanf("%d", &a[i].first);
for(int i=0; i < N; i++)
scanf("%d", &a[i].second);
for(int i=0; i < N; i++)
for(int j=0; j < N; j++)
d[i][j] = getDist(a[i], a[j]);
for(int i=0; i < M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
d[u][v] = 0;
d[v][u] = 0;
}
printf("%.10f\n", prim());
}
return 0;
}