4002

4002

Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 58   Accepted Submission(s) : 18
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.<br>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?<br>
 

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. <br><br>Input contains multiple test cases. Process to the end of file.<br>
 

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. <br>
 

Sample Input
  
  
3 1.0 1.0 2.0 2.0 2.0 4.0
 

Sample Output
  
  
3.41
 
题意:用最短的连线把所有的点连起来;


思路:大方向很好想,最小生成树+并查集;
      但是代码实现很困难。
      在网上找到了别人的代码。参考着写的。
      初始化的时候把边初始化为了0,出了很多问题;
AC代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;


const int N = 105;
struct Point {
    double x,y;
}p[N];
struct Edge{
    int s,e;
    double len;
}edge[N*(N-1)/2];
int father[N],n;
double dis(Point a,Point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int _find(int x){
    if(x==father[x]) return x;
    return _find(father[x]);
}
int cmp(Edge a,Edge b){
    return a.len<b.len;
}
double kruskal(int m){
    sort(edge+1,edge+m+1,cmp);
    double cost=0;
    for(int i=1;i<=m;i++){
        int x = _find(edge[i].s);
        int y = _find(edge[i].e);
        if(x!=y){
            father[x] = y;
            cost +=edge[i].len;
        }
    }
    return cost;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++) father[i] = i;
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&p[i].x,&p[i].y);
        }
        int m =1; ///边的数量
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){ ///这里是i+1开始,从0开始会多出很多边
                edge[m].s =i;
                edge[m].e = j;
                edge[m++].len = dis(p[i],p[j]);
            }
        }
        m--; ///记得
        printf("%.2lf\n",kruskal(m));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值