forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10369 Arctic Network.cpp
90 lines (73 loc) · 2.05 KB
/
10369 Arctic Network.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
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
int N;
int encounteredNum[505];
int squaredDist[505][505];
int xPos[505], yPos[505];
void DFS(int node, const int maxDist, const int attempt)
{
encounteredNum[node] = attempt;
for (int i = 0; i < N; ++i)
{
if (encounteredNum[i] != attempt && squaredDist[node][i] <= maxDist)
DFS(i, maxDist, attempt);
}
}
bool CanConnect(int outposts, const int maxDistance, const int attempt)
{
for (int i = 0; i < N && outposts >= 0; ++i)
{
if (encounteredNum[i] != attempt)
{
DFS(i, maxDistance, attempt);
--outposts;
}
}
return outposts >= 0;
}
inline int SquaredDist(int i, int j)
{
const int xDiff = xPos[i] - xPos[j], yDiff = yPos[i] - yPos[j];
return xDiff * xDiff + yDiff * yDiff;
}
int main()
{
int T;
cin >> T;
cout << fixed << setprecision(2);
while (T--)
{
int S;
cin >> S >> N;
vector<int> distances;
int numAttempts = 0;
for (int i = 0; i < N; ++i)
{
cin >> xPos[i] >> yPos[i];
encounteredNum[i] = numAttempts;
for (int j = 0; j < i; ++j)
{
squaredDist[i][j] = squaredDist[j][i] = SquaredDist(i, j);
distances.push_back(squaredDist[i][j]);
}
}
sort(distances.begin(), distances.end());
// Do binary search now
int start = 0, end = distances.size();
while (start != end)
{
int mid = (start + end) / 2;
const int allowedSquaredDist = distances[mid];
if (CanConnect(S, allowedSquaredDist, numAttempts))
end = mid; // Can possibly use less distance
else
start = mid + 1; // Will need more distance
++numAttempts;
}
cout << sqrt(distances[start]) << '\n';
}
}