Eddy's picture
http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1009&cid=12467&hide=0
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 8
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
Input contains multiple test cases. Process to the end of file.
Output
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
Author
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct{
double x,y;
int parent;
}point[105];
struct Edge{
int from,to;
double dis;
}edge[5100];
void init(int n){
for(int i=1;i<=n;i++)
point[i].parent=i;
}
int find(int x){
while(x!=point[x].parent)
x=point[x].parent;
return x;
}
void merge(int x,int y){
x=find(x);
y=find(y);
if(x!=y)
point[x].parent=y;
}
bool compare(Edge a,Edge b){
return a.dis<b.dis;
}
int main(){
int n;
double sum;
while(scanf("%d",&n)!=EOF){
float a,b;
int p=1;
init(n);
for(int i=1;i<=n;i++)
cin>>point[i].x>>point[i].y;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
edge[p].from=i;
edge[p].to=j;
edge[p].dis=sqrt( pow((point[i].x-point[j].x),2)+pow((point[i].y - point[j].y),2));
p++;
}
}
sort(edge+1,edge+p,compare);
int m=p-1,x,y;
sum=0.0;
for(int i=1;i<=m&&n>1;i++){
x=find(edge[i].from);
y=find(edge[i].to);
if(x!=y){
merge(x,y);
n--;
sum+=edge[i].dis;
}
}
printf("%.2lf\n",sum);
}
return 0;
}