-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva11228.cpp
150 lines (113 loc) · 2.29 KB
/
uva11228.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
123
124
125
126
127
128
129
130
131
132
133
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <limits.h>
#include <list>
#include <queue>
#include <map>
using namespace std;
typedef pair<int, int> ii;
#define TRvi(c, it) \
for(vector<pair<double, ii> >::iterator it = c.begin(); it != c.end(); it++)
#define BFS_WHITE -1
int N, insideCountry;
vector<ii> cities;
vector<pair<double, ii> > EdgeList;
int numDisjointSets;
double roadCost,railroadCost;
vector<int> pset; /* union-find disjoint sets */
double dist(ii a, ii b)
{
int x1 = a.first, y1 = a.second;
int x2 = b.first, y2 = b.second;
return hypot(x1-x2, y1-y2);
}
void initSets(int num)
{
pset.assign(num, 0);
numDisjointSets = num;
for(int i = 0; i < num; i++)
pset[i] = i;
}
int findSet(int i)
{
if(pset[i] == i)
return i;
else
return pset[i] = findSet(pset[i]);
}
bool isSameSet(int i, int j)
{
return findSet(i) == findSet(j);
}
void unionSet(int i, int j)
{
if(!isSameSet(i, j))
numDisjointSets -= 1;
pset[findSet(i)] = findSet(j);
}
void Kruskal()
{
initSets(N);
EdgeList.clear();
for(int src = 0; src < N; src++)
{
for(int dest = src+1; dest < N; dest++)
{
EdgeList.push_back(make_pair(dist(cities[src], cities[dest]), ii(src, dest)));
}
}
sort(EdgeList.begin(), EdgeList.end());
roadCost = 0; railroadCost = 0;
int E = EdgeList.size();
for(int i = 0; i < E; i++)
{
pair<double, ii> front = EdgeList[i];
int u = front.second.first, v = front.second.second;
double cost = front.first;
if(!isSameSet(u, v))
{
if(cost > insideCountry)
railroadCost += cost;
else
roadCost += cost;
unionSet(u, v);
}
}
}
int main()
{
int numCountries;
int x, y;
int Cases; cin >> Cases;
for(int CaseNo = 1; CaseNo <= Cases; CaseNo++)
{
cin >> N >> insideCountry;
cities.clear();
cities.resize(N);
for(int i = 0; i < N; i++)
{
cin >> x >> y;
cities[i].first = x;
cities[i].second = y;
}
initSets(N);
for(int u = 0; u < N; u++)
{
for(int v = u+1; v < N; v++)
{
if(dist(cities[u], cities[v]) <= insideCountry)
unionSet(u, v);
}
}
numCountries = numDisjointSets;
Kruskal();
printf("Case #%d: %d %d %d\n", CaseNo, numCountries, (int)(roadCost + 0.5), (int)(railroadCost + 0.5));
}
return 0;
}