-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclustering.cpp
More file actions
executable file
·75 lines (75 loc) · 1.48 KB
/
clustering.cpp
File metadata and controls
executable file
·75 lines (75 loc) · 1.48 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
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MAX 1000000
long long int id[MAX],n,edges;
pair<long double, pair<long long int, long long int> > p[MAX];
void initialize()
{
for(ll i=0;i<MAX;++i)
{
id[i]=i;
}
}
ll root(ll x)
{
while(id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
}
void union1(ll x, ll y)
{
int p = root(x);
int q = root(y);
id[p] = id[q];
}
vector<long double> kruskal(pair<long double, pair<ll, ll> > p[])
{
ll x, y;
vector<long double> vect;
long double cost;
for(ll i=0;i<edges;i++)
{
x=p[i].second.first;
y=p[i].second.second;
cost=p[i].first;
if(root(x)!=root(y))
{
vect.push_back(cost);
union1(x,y);
}
}
return vect;
}
int main()
{
ll n,kk,i,j,k;
ll weight, cost, minimumCost;
long double dist;
initialize();
cin>>n;
ll a[n],b[n];
edges=(n*(n-1))/2;
for(i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
}
cin>>kk;
k=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
dist=sqrt(((a[i]-a[j])*(a[i]-a[j]))+((b[i]-b[j])*(b[i]-b[j])));
p[k]=make_pair(dist,make_pair(i,j));
k++;
}
}
sort(p,p+edges);
vector<long double> vect=kruskal(p);
cout<<fixed<<setprecision(12)<<vect[n-kk];
return 0;
}