Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10049 Accepted Submission(s): 5072
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?
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#define MaxV 100005
#define MaxE 200005
using namespace std;
struct edge{
int u,v;
double cost;
}e[MaxE];
struct Node{
double x,y;
}node[105];
int fa[MaxV];
bool cmp(edge a,edge b)
{
return a.cost<b.cost;
}
int findfa(int x)
{
int a=x;
while(x!=fa[x])
{
x=fa[x];
}
while(a!=fa[a]) //路徑壓縮
{
int z=a;
a=fa[a];
fa[z]=x;
}
return x;
}
double kru(int n,int m)
{
double ans1=0;
int num_edge=0;
for(int i=0;i<=n;i++)
{
fa[i]=i;
}
sort(e,e+m,cmp);
for(int i=0;i<m;i++)
{
int fau=findfa(e[i].u);
int fav=findfa(e[i].v);
if(fau!=fav)
{
fa[fau]=fav;
ans1=ans1+e[i].cost;
num_edge++;
if(num_edge==n-1) break;
}
}
return ans1;
}
int main()
{
int n,m,x;
while(~scanf("%d",&n))
{
m=(n*(n-1))/2;
for(int i=1;i<=n;i++)
{
scanf("%lf %lf",&node[i].x,&node[i].y);
}
int k=0;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
e[k].u=i;
e[k].v=j;
e[k].cost=sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y));
k++;
}
}
double ans=kru(n,m);
printf("%.2lf\n",ans);
}
return 0;
}
Input contains multiple test cases. Process to the end of file.