zoukankan      html  css  js  c++  java
  • UVALive 4872 Underground Cables 最小生成树

    J - Underground Cables
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

    Description

    Download as PDF

    A city wants to get rid of their unsightly power poles by moving their power cables underground. They have a list of points that all need to be connected, but they have some limitations. Their tunneling equipment can only move in straight lines between points. They only have room for one underground cable at any location except at the given points, so no two cables can cross.

    Given a list of points, what is the least amount of cable necessary to make sure that every pair of points is connected, either directly, or indirectly through other points?

    Input

    There will be several test cases in the input. Each test case will begin with an integer N(2$ le$N$ le$1, 000), which is the number of points in the city. On each of the next N lines will be two integers, X and Y(- 1, 000$ le$X, Y$ le$1, 000), which are the (X, Y) locations of the N points. Within a test case, all points will be distinct. The input will end with a line with a single 0.

    Output

    For each test case, output a single real number, representing the least amount of cable the city will need to connect all of its points. Print this number with exactly two decimal places, rounded. Print each number on its own line with no spaces. Do not print any blank lines between answers.

    Sample Input

    4 
    0 0 
    0 10 
    10 0 
    10 10 
    2 
    0 0 
    10 10 
    0
    

    Sample Output

    30.00 
    14.14

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    
    typedef long long ll;
    using namespace std;
    
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 1010
    const int inf=1000000;   //无限大
    int u[maxn*maxn],v[maxn*maxn];
    double w[maxn*maxn];
    int r[maxn*maxn];
    //两个端点存在u和v数组中,边权存在w数组中
    int p[maxn*maxn];
    int n,m;
    int cmp(const int i,const int j)
    {
        return w[i]<w[j];
    } //间接排序函数
    int find(int x)
    {
        return p[x]==x?x:p[x]=find(p[x]);
    }
    double Kruskal()
    {
        double ans=0;
        for(int i=0;i<n;i++) p[i]=i;//初始化并查集
        for(int j=0;j<m;j++) r[j]=j;//初始化边序号
        sort(r,r+m,cmp);
        for(int i=0;i<m;i++)
        {
            int e=r[i];
            int x=find(u[e]);
            int y=find(v[e]);
            //找到当前边两个端点所在的集合编号
            if(x!=y)
            {
                ans+=w[e];
                p[x]=y;
            }
            //如果在不同集合,合并
        }
        return ans;
    }
    
    struct node
    {
        double x;
        double y;
    };
    
    node kill[maxn];
    
    int main()
    {
        int t;
        while(cin>>t){
    
        if(t==0)
            break;
        n=0,m=0;
        n=t;
        double kiss;
    
        for(int i=0;i<n;i++)
        {
            cin>>kill[i].x>>kill[i].y;
        }
    
        for(int i=0;i<t;i++)
        {
            for(int j=i+1;j<t;j++)
            {
                kiss=sqrt((kill[i].x-kill[j].x)*(kill[i].x-kill[j].x)+(kill[i].y-kill[j].y)*(kill[i].y-kill[j].y));
                u[m]=i;
                v[m]=j;
                w[m++]=kiss;
    
            }
        }
    
        printf("%.2lf
    ",Kruskal());
        }
        return 0;
    }
  • 相关阅读:
    201671010131 2016-2017-2 《Java程序设计》逐渐的进步。
    201671010131 2016-2017-2 《Java程序设计》走向核心。
    201671010131 2016-2017-2 《Java程序设计》艰难的旅程.
    201671010131 2016-2017-2 《Java程序设计》第二周 由简入繁的开始。
    201671010131 2016-2017-2 《Java程序设计》初学Java,所感所学总结。
    ajax提交请求数组时,参数名带[]
    天气接口-高德api
    Lambda 表达式在线程中的使用
    Centos7 下安装和配置 MinDoc
    天气接口 乱码问题
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4238223.html
Copyright © 2011-2022 走看看