forked from fatemehkarimi/uvaSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-11228.cpp
111 lines (82 loc) · 1.98 KB
/
uva-11228.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
//uva 11228
//Transportation system
#include <iostream>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
int parent(vector <int> & ufds, int a);
void Union(vector <int> & ufds, int a, int b);
double distance(pair <int, int> x, pair <int, int> y);
int main(void)
{
int T = 0;
cin >> T;
for(int tcounter = 1; tcounter <= T; ++tcounter){
int n, r;
cin >> n >> r;
vector < pair <int, int> > cities(n);
for(int i = 0; i < n; ++i){
int x, y;
cin >> x >> y;
cities[i] = make_pair(x, y);
}
set < pair <double, pair <int, int> > > KruskalSet;
vector <int> states(n, -1);
for(int i = 0; i < n; ++i)
for(int j = i + 1; j < n; ++j){
double dis = distance(cities[i], cities[j]);
KruskalSet.insert(make_pair(dis, make_pair(i, j)));
if(dis <= (double)r)
Union(states, i, j);
}
int n_state = 0;
for(auto state : states)
if(state == -1)
++n_state;
double roads = 0;
double railroads = 0;
vector <int> ufds(n, -1);
while(!KruskalSet.empty()){
double dis = KruskalSet.begin()->first;
int i = KruskalSet.begin()->second.first;
int j = KruskalSet.begin()->second.second;
KruskalSet.erase(KruskalSet.begin());
if(parent(ufds, i) != parent(ufds, j)){
Union(ufds, i, j);
if(parent(states, i) == parent(states, j))
roads += dis;
else
railroads += dis;
}
}
cout << "Case #" << tcounter << ": " << n_state << ' ' << round(roads) << ' ' << round(railroads) << endl;
}
return 0;
}
double distance(pair <int, int> x, pair <int, int> y)
{
int tmp = (x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second);
return sqrt(tmp);
}
void Union(vector <int> & ufds, int a, int b)
{
int p1 = parent(ufds, a);
int p2 = parent(ufds, b);
if(p1 == p2)
return;
ufds[p2] = p1;
return;
}
int parent(vector <int> & ufds, int a)
{
int x = a;
while(ufds[x] != -1)
x = ufds[x];
while(ufds[a] != -1){
int tmp = ufds[a];
ufds[a] = x;
a = tmp;
}
return x;
}