-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10021-2.cpp
More file actions
99 lines (93 loc) · 1.92 KB
/
10021-2.cpp
File metadata and controls
99 lines (93 loc) · 1.92 KB
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
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ft first
#define sd second
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvll = vector<vll>;
using vvpii = vector<vpii>;
using vvpll = vector<vpll>;
using vvs = vector<vs>;
vpii point;
int p[2001];
int find(int x){
if(p[x]==x)
return x;
return p[x] = find(p[x]);
}
void Union(int a , int b){
a = find(a);
b = find(b);
if(a!=b){
p[b]=a;
}
}
int getDist(pii a, pii b){
return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second);
}
struct edge{
int a;
int b;
int dist;
edge(int x,int y,int z):a(x),b(y),dist(z){};
};
struct compare{
bool operator()(struct edge a,struct edge b){
return a.dist>b.dist;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,l,x,y;
cin>>n>>l;
for(int i=0;i<n;i++){
cin>>x>>y;
point.emplace_back(x,y);
}
for(int i=0;i<n;i++){
p[i]=i;
}
priority_queue <edge ,vector<edge>,compare> pq;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int dist = getDist(point[i],point[j]);
if(dist>=l)
pq.push({i,j,dist});
}
}
int cnt=0;
ll ans=0;
while(!pq.empty()){
auto t = pq.top();
pq.pop();
int a = t.a;
int b = t.b;
int dist = t.dist;
if(find(a)!=find(b)){
ans+=dist;
cnt++;
Union(a,b);
}
if(cnt==n-1){
break;
}
}
if(cnt==n-1){
cout<<ans;
}else{
cout<<"-1";
}
}