HDU1162 Eddy's picture

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

题意:埃迪最近开始喜欢画画,他很确定自己会成为画家。每天,埃迪都会在自己的小房间里画画,他通常会放出最新的画来让他
的朋友欣赏。但是结果是可以想像的,朋友们对他的画不感兴趣。埃迪感到非常困惑,为了将所有朋友的看法改变为他的绘画技术,
所以埃迪给你的朋友们带来了麻烦。 问题描述如下:给定图纸上的一些坐标点,每个点都与墨水用直线链接,导致所有点最终链接
在同一位置。您的职责发现墨迹绘制的最短距离是多少个距离?
输入:第一行包含0 <n <= 100点的数量。对于每个点,都会跟随一条线;接下来的每一行包含两个实数,表示该点的(x,y)坐
标。输入包含多个测试用例。处理到文件末尾。
输出:您的程序将一个实数打印到两位小数:可以连接所有点的墨水线的最小总长度。

标签:最小生成树
Kruskal算法:
我们从最小边权的边开始,按边权从小到大依次加入,如果某次加边产生了环,就扔掉这条边,直到加入了n-1条边,即形成了一棵树。
Prim算法
从任意一个结点开始,将结点分成两类:已加入的,未加入的。每次从未加入的结点中,找一个与已加入的结点之间边权最小值最小的结点。
然后将这个结点加入,并连上那条边权最小的边。重复n-1次。
本题我用的是kruskal算法
代码如下:
 
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<math.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int book[110];  //作为标记数组 
 7 struct node{
 8     int start,end;    //起点,终点,以及距离 
 9     double vlu;
10 }stu[10010];
11 int cmp(node a,node b){
12     return a.vlu<b.vlu;
13 }
14 int find(int m){
15     int p=m;
16     while(p!=book[p])
17     p=book[p];
18     return p;
19 }
20 int join(int pa,int pb){
21     int na=find(pa);
22     int nb=find(pb);
23     if(na!=nb){  //判断是不是环 
24         book[na]=nb;
25         return 1;
26     }
27     return 0;
28 }
29 int main(){
30     int n;
31     double x[110],y[110];
32     while(scanf("%d",&n)!=EOF){
33         for(int i=0;i<110;i++){
34             book[i]=i;
35         }
36         for(int i=0;i<n;i++){
37             scanf("%lf%lf",&x[i],&y[i]);
38         }
39         int k=0;
40         for(int i=0;i<n;i++){
41             for(int j=i+1;j<n;j++){
42                 stu[k].start=i;
43                 stu[k].end=j;
44                 stu[k].vlu=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
45                 k++;
46             }    
47         }
48         sort(stu,stu+k,cmp); //排序  边值从小到大依次加入 
49         double sum=0.00;
50         for(int i=0;i<k;i++){
51             if(join(stu[i].start,stu[i].end))
52             sum+=stu[i].vlu;
53         }
54         printf("%.2lf\n",sum);
55     }
56     return 0;
57 } 
 

猜你喜欢

转载自www.cnblogs.com/linda-/p/13381999.html