-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11228.cpp
95 lines (90 loc) · 1.74 KB
/
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
/*
11228 - Transportation system.
*/
#include<bits/stdc++.h>
using namespace std;
vector<pair<double,pair<long,long>>>p;
const int MX = 1e6+5;
long x[1005],y[1005];
long parent[MX];
int n,r;
long findd(long i)
{
return (parent[i]==i)?i:findd(parent[i]);
}
void initializer(long n)
{
for(int i=0; i<n; i++)
{
parent[i]=i;
}
}
int main()
{
///freopen("input.txt", "r", stdin);
///freopen("output.txt", "w", stdout);
int t,tc=0;
cin>>t;
while(t--)
{
cin>>n>>r;
initializer(n);
p.clear();
for(int i=0; i<n; i++)
{
cin>>x[i]>>y[i];
}
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
double diff = sqrt((x[i]-x[j])*(x[i]-x[j])*1.0+(y[i]-y[j])*(y[i]-y[j])*1.0);
p.push_back(make_pair(diff,make_pair(i,j)));
}
}
sort(p.begin(),p.end());
double rail=0,road=0,state=1;
for(int i=0; i<p.size(); i++)
{
int px = findd(p[i].second.first);
int py = findd(p[i].second.second);
double cost = p[i].first;
if(px!=py && cost>r*1.0)
{
parent[px]=py;
rail+=cost;
state++;
}
else if(px!=py)
{
parent[px]=py;
road+=cost;
}
}
long long road_len = floor(road+.5);
long long rail_len = floor(rail+.5);
cout<<"Case #"<<(++tc)<<": "<<state<<" "<<road_len<<" "<<rail_len<<endl;
}
return 0;
}
/*
Sample Input
3
3 100
0 0
1 0
2 0
3 1
0 0
100 0
200 0
4 20
0 0
40 30
30 30
10 10
Sample Output
Case #1: 1 2 0
Case #2: 3 0 200
Case #3: 2 24 28
*/