六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 26|回复: 0

杭电1162 最小生成树问题

[复制链接]

升级  84%

12

主题

12

主题

12

主题

童生

Rank: 1

积分
42
 楼主| 发表于 2013-1-26 12:28:47 | 显示全部楼层 |阅读模式
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162
Problem Description:
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
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:
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.



Output:
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.



Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0


Sample Output
3.41

AC代码····
***************************************************
#include"stdio.h"
#include"math.h"
#include"string.h"
#define MAX 11111111
double map[101][101],x[101],y[101];
int n,father[101];
void Make_map()
{
int i,j;
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
{
if(i==j)
map[i][j]=MAX;
else
map[i][j]=map[j][i]=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
}
}
void Make_father()
{
int i;
for(i=1;i<=n;i++)
father[i]=i;
}
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void Union(int x,int y)
{
int i;
x=find(x);
y=find(y);
if(x!=y)
{
for(i=1;i<=n;i++)
if(father[i]==y)
father[i]=x;
father[y]=x;
}
}
int main()
{
int i,j,k,a,b;
double min,sum;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
Make_map();
Make_father(n);
sum=0;
for(k=1;k<=n;k++)
{
min=MAX;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(map[i][j]<min&&father[i]!=father[j])
{
min=map[i][j];
a=i;
b=j;
}
}
}
if(father[a]!=father[b])
{
sum+=map[a][b];
Union(a,b);
map[a][b]=map[b][a]=MAX;
}
}
printf("%.2lf\n",sum);
}
return 0;
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表