-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathransportationSystem_UVA11228OJ.cpp
122 lines (104 loc) · 2.51 KB
/
ransportationSystem_UVA11228OJ.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
using namespace std;
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;
}
}
void _union(int u, int v, vector<int> & p)
{
p[_find(u, p)] = _find(v, p);
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int testCase; cin >> testCase;
for (int c = 1; c <= testCase; ++c)
{
int n, r; cin >> n >> r;
vector<point> cities;
vector<int> p(n);
vector<edge> ways;
for (int k = 0; k <= n - 1; ++k)
{
int y; int x; cin >> y >> x;
point now{y, x};
for (int i = 0; i <= k - 1; ++i)
{
edge temp{i, k, distance(now, cities[i])};
ways.push_back(temp);
}
cities.push_back(now);
}
sort(ways.begin(), ways.end());
for (int k = 0; k <= n - 1; ++k)
{
p[k] = k;
}
int sizeWays = ways.size(), totalStates = 1;
double lengthRoads = 0.0, lengthRailroads = 0.0;
for (int i = 0, j = 0; i <= n - 2 && j <= sizeWays - 1; ++j)
{
if (_find(ways[j].u, p) == _find(ways[j].v, p)) continue;
_union(ways[j].u, ways[j].v, p);
if (ways[j].d >= r)
{
++totalStates;
lengthRailroads += ways[j].d;
}
else
{
lengthRoads += ways[j].d;
}
++i;
}
int out1 = (int)(lengthRoads + 0.5), out2 = (int)(lengthRailroads + 0.5);
cout << "Case #" << c << ": " << totalStates << ' ' << out1 << ' ' << out2 << '\n';
}
cout.flush();
return 0;
}