题目链接: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;
}
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;
}
探讨了如何通过连线将平面上的多个点以最短总距离连接起来的问题,使用了图论中的Kruskal算法实现。介绍了算法的具体实现过程,并提供了一段C++代码作为解决方案。
559

被折叠的 条评论
为什么被折叠?



