-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharcticNetwork_UVA10369.cpp
116 lines (100 loc) · 2.27 KB
/
arcticNetwork_UVA10369.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
ifstream fin("arcticNetwork_UVA10369.in");
ofstream fout("arcticNetwork_UVA10369.out");
struct point
{
int y;
int x;
bool operator < (const point & temp) const
{
return y < temp.y && x < temp.x;
}
};
struct edge
{
int u;
int v;
double d;
bool operator < (const edge & temp) const
{
return d < temp.d;
}
};
double distance(point & u, point & v)
{
return sqrt((u.y - v.y) * (u.y - v.y) + (u.x - v.x) * (u.x - v.x));
}
int _find(int u, vector<int> & p)
{
if (p[u] == u)
{
return u;
}
else
{
int ans = _find(p[u], p);
p[u] = ans;
return ans;
}
}
int main()
{
int testCase; fin >> testCase;
fout << fixed << setprecision(2);
for (int c0 = 1; c0 <= testCase; ++c0)
{
int st, rt; fin >> st >> rt;
vector<point> radios;
vector<int> p(rt);
vector<edge> ways, picked;
for (int rc = 0; rc <= rt - 1; ++rc)
{
int y, x; fin >> y >> x;
point now{y, x};
for (int i = 0; i <= rc - 1; ++i)
{
edge temp{i, rc, distance(now, radios[i])};
ways.push_back(temp);
}
radios.push_back(now);
}
sort(ways.begin(), ways.end());
for (int i = 0; i <= rt - 1; ++i)
{
p[i] = i;
}
int sizeWays = ways.size();
for (int i = 0, j = 0; i <= rt - 2 && j <= sizeWays - 1; ++j)
{
int res0 = _find(ways[j].u, p), res1 = _find(ways[j].v, p);
if (res0 == res1)
{
continue;
}
p[res0] = res1;
picked.push_back(ways[j]);
++i;
}
if (st == 0) st = 1;
int pos = picked.size() - st;
if (pos < 0)
{
fout << "0.00\n";
}
else
{
fout << picked[pos].d << '\n';
}
}
return 0;
}