-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10021.cpp
50 lines (50 loc) · 1.25 KB
/
10021.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
#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define pii pair<int,int>
using namespace std;
int n, c, ans;
pii farm[2000];
int parent[2000];
int find(int cur) {
if(parent[cur] == cur) return cur;
return parent[cur] = find(parent[cur]);
}
bool isGroup(int a, int b) {
a = find(a); b = find(b);
if(a == b) return 1;
if(a > b) parent[b] = a;
else parent[a] = b;
return 0;
}
int main() {
cin >> n >> c;
for(int i = 0; i < n; i++) parent[i] = i;
for(int i = 0; i < n; i++) {
int a, b; cin >> a >> b;
farm[i] = {a, b};
}
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
int xDist = pow(farm[i].X - farm[j].X, 2);
int yDist = pow(farm[i].Y - farm[j].Y, 2);
if(xDist + yDist >= c) {
pq.push({xDist + yDist, {i, j}});
}
}
}
int cnt = 0;
while(!pq.empty()) {
auto temp = pq.top(); pq.pop();
pii cur = temp.Y;
int cost = temp.X;
if(!isGroup(cur.X, cur.Y)) {
cnt++;
ans += cost;
}
}
if(cnt < n - 1) cout << -1;
else cout << ans;
}