• hdu 1162 Eddy's picture


    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
     
    Author
    eddy

     最小生成树的题目,还是keuskal

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int MAX=1000000;
    struct node
    {
        int left,right;
        double cost;
    }road[MAX];
    
    bool cmp(node x,node y){return x.cost<y.cost;}
    
    int p[MAX],m,n;
    
    int find(int x){return x==p[x]? x:p[x]=find(p[x]);}
    
    double kruskal()
    {
        double ans=0.0;
        int k=0;
        for (int i=0;i<=n;i++) p[i]=i;
        for (int i=0;i<m;i++)
        {
            int x=find(road[i].left);
            int y=find(road[i].right);
            if(x!=y)
            {
                ans+=road[i].cost;
                p[x]=y;
                k++;
            }
            if (k==n-1) break;
        }
        if (k==n-1)return ans;
        else return -1;
    }
    
    int main()
    {
        //int n;
        double x[101],y[101];
        while (scanf("%d",&n)!=EOF)
        {
            for (int i=1;i<=n;i++)scanf("%lf%lf",&x[i],&y[i]);
            m=0;
            for (int i=1;i<=n;i++)
            for (int j=1;j<i;j++)
            {
                    road[m].left=i;
                    road[m].right=j;
                    road[m].cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                    m++;
            }
            sort(road,road+m,cmp);
            printf("%.2lf
    ",kruskal());
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    [FE] uni-app 安装 uview-ui 的两种方式
    [FE] Canvas 转图片并下载的方式
    [K8s] Pod 与容器设计模式 Sidecar
    [Docker] 使 Volume 独立于容器运行时的方式
    mysql授权用户以指定IP登录的方法
    linux安装mysql客户端
    kali 安装 Google Chrome
    KALI图形界面root 用户登入
    git reset 版本回退命令
    git log 常用命令
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3719976.html
Copyright © 2020-2023  润新知