-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva_10034_Freckles.cpp
More file actions
110 lines (101 loc) · 2.36 KB
/
uva_10034_Freckles.cpp
File metadata and controls
110 lines (101 loc) · 2.36 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
100
101
102
103
104
105
106
107
108
109
110
#include<bits/stdc++.h>
#define pb(n) push_back(n)
#define bug cout<<"bug";
#define all(c) c.begin(),c.end()
#define fr(i,n) for(i=0;i<n;i++)
#define sf(n) scanf("%d",&n)
#define mst(n) memset(n,0,sizeof(n))
#define nl printf("\n")
#define lli long long int
#define tr(container,it) \
for (auto it = container.begin(); it != container.end() ; ++it)
#define INF INT_MAX
#define MOD 1000000007
#define mp(i,j) make_pair(i,j)
#define infile freopen("in.txt","r",stdin)
#define outfile freopen("out.txt","w",stdout)
#define pr(a) cout<<cout<<a.a.first<<" "<< a.a.second<<endl
#define V 5
using namespace std;
typedef pair<double,double> pi;
typedef struct Edge
{
pi u,v;
double w;
Edge(pi a,pi b)
{
u=a, v=b;
};
bool operator<(const Edge& a) const
{
return w< a.w;
};
} edge;
int parent(int pr[],int a)
{
if(pr[a]==a) return a;
pr[a]=parent(pr,pr[a]);
return pr[a];
}
double weight(edge a)
{
return (sqrt((a.u.first-a.v.first)*(a.u.first-a.v.first)+(a.u.second-a.v.second)*(a.u.second-a.v.second)));
}
double kruskal(vector<edge> &e,vector<pi> &v,int ver)
{
int num_edge=(ver*(ver-1))/2; // number of edges is = ver C 2;
sort(all(e));
int assin=0;
int pr[ver+3];
map<pi,int> imap;
bool vis[ver+3]= {};
mst(vis);
for(int i=0; i<ver; i++)
{
imap[v[i]]=i;
pr[i]=i;
}
double cost=0;
for(int i=0; i<num_edge; i++)
{
pi a=e[i].u, b=e[i].v;
int num1=parent(pr,imap[a]), num2= parent(pr,imap[b]);
if(num1!=num2)
{
pr[num1]=num2;
cost+=e[i].w;
}
}
return cost;
}
int main()
{
int test;
sf(test);
bool nel=0;
while(test--)
{
if(nel)
nl;
nel=1;
int ver;
sf(ver);
vector<edge> e;
vector<pi> str;
for(int i=0; i<ver; i++)
{
pi a;
scanf("%lf %lf",&a.first,&a.second);
str.push_back(a);
for(int j=0; j<i; j++)
{
edge tmp(str[j],a);
tmp.w=weight(tmp);
e.push_back(tmp);
}
}
double cost=kruskal(e,str,ver);
e.clear(), str.clear();
printf("%.2lf\n",cost);
}
}