zoukankan      html  css  js  c++  java
  • hdu 1162(最小生成树)

    Eddy's picture

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8797    Accepted Submission(s): 4476


    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
    题意:大意就是给你n个点求出这n个点形成的最小生成树。
    题解:kruskal算法解。。开始的时候将边初始化成了0,WA了两次..好久没打过生疏了。
    #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
    ",kruskal(m));
        }
    }
  • 相关阅读:
    Python 元胞自动机模拟——生命游戏
    ()python画动态图——plt.ion动图使用,训练过程展示
    Python——因子分析(KMO检验和Bartlett's球形检验)
    bind(),unbind(),hover(),toggle(),animate()
    多计算机通信中的时间同步问题
    如何创建自定义尺寸的空白地图
    ROS中的珊格地图——nav_msgs::OccupancyGrid
    c++使用eigen库,矩阵维度错误
    Eigen 求最小二乘
    Mybatis 框架下 SQL 注入攻击的 3 种方式,真是防不胜防!
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5469166.html
Copyright © 2011-2022 走看看