forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10369.cpp
128 lines (113 loc) · 1.58 KB
/
10369.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
template <class T>
string toStr(const T &x)
{
stringstream s;
s << x;
return s.str();
}
template <class T>
int toInt(const T &x)
{
stringstream s;
s << x;
int r;
s >> r;
return r;
}
struct Edge
{
int a, b;
double w;
Edge(int A, int B, double W)
: a(A), b(B), w(W)
{
}
bool operator<(const Edge &other) const
{
return w < other.w;
}
};
struct Point
{
int x, y;
Point(int X, int Y)
: x(X), y(Y)
{
}
};
double dist(const Point &a, const Point &b)
{
return hypot(a.x - b.x, a.y - b.y);
}
const int MAXN = 505;
int T, s, n, p[MAXN];
vector<Edge> edges;
vector<Point> points;
void init()
{
for (int i = 0; i <= n; i++)
{
p[i] = i;
}
}
int find_(int u)
{
if (p[u] == u)
{
return u;
}
return p[u] = find_(p[u]);
}
void join(int u, int v)
{
int a = find_(u), b = find_(v);
if (a == b)
{
return;
}
p[a] = b;
}
double kruskal()
{
init();
sort(edges.begin(), edges.end());
int times = 0;
double ans = 0.0f;
for (int i = 0; i < edges.size() && times < n - s; i++)
{
int u = edges[i].a, v = edges[i].b;
double w = edges[i].w;
if (find_(u) != find_(v))
{
ans = w;
times++;
join(u, v);
}
}
return ans;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &s,&n);
edges.clear();
points.clear();
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%d%d", &x,&y);
points.push_back(Point(x, y));
for (int j = 0; j < i; j++)
{
edges.push_back(Edge(j, i, dist(points[j], points[i])));
}
}
printf("%.2lf\n", kruskal());
}
return 0;
}