-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainsSpanningTree.cpp
More file actions
58 lines (54 loc) · 1.19 KB
/
containsSpanningTree.cpp
File metadata and controls
58 lines (54 loc) · 1.19 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
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
double minDist,dist,px[103],py[103],sum;
double findDist(int i,int j){
double dx=px[i]-px[j],dy=py[i]-py[j];
// cout<<"["<<dx<<","<<dy<<"]"<<endl;
return sqrt(dx*dx+dy*dy);
}
int main(){
int q;
int n,i,j,k,minInd;
bool connected[103];
scanf("%d",&q);
while(q--){
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%lf %lf",&px[i],&py[i]);
// cout<<px[i]<<","<<py[i]<<endl;
}
memset(connected,0,sizeof(connected));
sum=0;
connected[0]=1;
for(k=1;k<n;k++){
minInd=0;
for(i=0;i<n;i++){
if(!connected[i])continue;
for(j=0;j<n;j++){
if(connected[j])continue;
dist=findDist(i,j);
// cout<<dist<<"("<<i<<","<<j<<")"<<endl;
if(minInd){
if(minDist>dist){
minDist=dist;
minInd=j;
}
}else{
minInd=j;
minDist=dist;//findDist(i,j);
}
}
}
// cout<<minDist<<"to"<<minInd<<endl;
sum+=minDist;
connected[minInd]=1;
}
printf("%.2f\n",sum);
if(q)printf("\n");
}
return 0;
}