-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSolution.cpp
More file actions
96 lines (89 loc) · 1.8 KB
/
Solution.cpp
File metadata and controls
96 lines (89 loc) · 1.8 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
#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
double points[N][2];
int pr[N];
int sz[N];
struct edge
{
int u, v;
double w;
bool operator<(const edge &p) const
{
return w < p.w;
}
};
vector<edge>e;
void makeSet(int n)
{
for(int i = 0; i < n; i++)
{
pr[i] = i;
sz[i] = 1;
}
}
int findSet(int v)
{
if(v == pr[v])
return v;
return pr[v] = findSet(pr[v]);
}
void kruskal(int n, int r)
{
double roadLen, railLen;
roadLen = railLen = 0;
int roadCount = 0;
makeSet(n);
sort(e.begin(), e.end());
for(int i = 0; i < e.size(); i++)
{
int u = findSet(e[i].u);
int v = findSet(e[i].v);
if(u != v)
{
if(e[i].w <= r)
{
roadCount++;
roadLen += e[i].w;
}
else
railLen += e[i].w;
if(sz[u] < sz[v])
swap(u, v);
pr[v] = u;
}
}
cout << (n - roadCount) << " ";
cout << fixed << setprecision(0) << roadLen << " " << railLen << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int cc = 0;
int t;
cin >> t;
while(t--)
{
int n, r;
cin >> n >> r;
for(int i = 0; i < n; i++)
cin >> points[i][0] >> points[i][1];
for(int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
edge tp;
tp.u = i;
tp.v = j;
tp.w = sqrt(pow(points[i][0] - points[j][0], 2) + pow(points[i][1] - points[j][1], 2));
e.push_back(tp);
}
}
cout << "Case #" << (cc + 1) << ": ";
cc++;
kruskal(n, r);
e.clear();
}
return 0;
}