现在的位置: 首页 > 综合 > 正文

HDU 1162 Eddy’s picture

2013年10月14日 ⁄ 综合 ⁄ 共 1940字 ⁄ 字号 评论关闭

Problem Description

Eddy begins to like paintingpictures recently ,he is sure of himself to become a painter.Every day Eddydraws pictures in his small room, and he usually puts out his newest picturesto let his friends appreciate. but
the result it can be imagined, the friendsare not interested in his picture.Eddy feels very puzzled,in order to changeall friends 's view to his technical of painting pictures ,so Eddy creates aproblem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawingpaper, every point links with the ink with the straight line, causes all pointsfinally to link in the same place. How many distants does your duty discoverthe shortest length which
the ink draws?

 

Input

The first line contains 0 <n <= 100, the number of point. For each point, a line follows; eachfollowing line contains two real numbers indicating the (x,y) coordinates ofthe point.

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

 

Output

Your program prints a singlereal number to two decimal places: the minimum total length of ink lines thatcan connect all the points.

 

Sample Input

3

1.0 1.0                       

2.0 2.0

2.0 4.0

 

Sample Output

3.41

 

 

 

题目简介:给定一些点的坐标,将这些点连接起来最小的距离。

方法:最小生成树。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int n, m;
int set[100];
float result;

struct d
{
	float x, y;
}D[110];//保存点的坐标

struct node 
{
	int u, v;
	float w;
}T[5000];//保存两点之间的距离

 int cmp(const void *a,const void *b)
 {
	 struct node *c,*d;
	 c = (struct node *)a;
	 d = (struct node *)b;
	 return c->w > d->w ?1 :-1;//因为w是double型,所以不能返回w.
 };

int root(int x)
{
    if(set[x]==x)
        return x;
    set[x] = root(set[x]);
    return set[x];
};
 
 int main()
 {
     int j, k, i, x1, y1;
     while(scanf("%d", &n)!=EOF)
     {

		    for(i = 0;i<n;i++)
			{
				set[i] = i;
			}//初始化并查集

            i = 0;
			k = 0;
			result = 0;//初始化
			scanf("%f%f",&D[i].x, &D[i].y);
            for(i = 1;i <n;i++ )
            {
              scanf("%f%f",&D[i].x, &D[i].y );
			  for(j = 0;j<i;j++)
			  {
				  T[k].u = j;
				  T[k].v = i;
				  T[k].w =sqrt((D[i].y -D[j].y)*(D[i].y - D[j].y)+(D[i].x-D[j].x)*(D[i].x-D[j].x));//计算输入点与之前点的距离
				  k++;
			  }
            }

			//处理
            qsort( T, k,sizeof(T[0]), cmp);
			k = 0;
			for(i=0;i<n*(n-1)/2;i++)
			{

				x1 = root(T[i].v);
				y1 = root(T[i].u);
				if(x1!=y1)
				{
					result+=T[i].w;
					set[x1] = y1;
				}
			}


			//输出
            printf("%.2lf\n", result);
     }
	 system("pause");
	 return 0;
}

 

【上篇】
【下篇】

抱歉!评论已关闭.