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;
    }
  • 相关阅读:
    MySQL 安装和配置
    其它 Google Chrome 已停止工作
    VUE 父组件和子组件相互传值 组件之间的数据传递
    SpringBlade 源码 个性化修改 添加字典时 自动填充字典值
    MyBatis-Plus SpringBlade 生成代码时 啥内容都没有 只有目录
    Oracle IIS部署报错
    pycharm连接sqlite后打开db文件不显示表的问题
    Hbase集群搭建以及启动(单点启动,群起)
    Flume的put和take事务
    稀疏数组学习
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4238223.html
Copyright © 2011-2022 走看看