zoukankan      html  css  js  c++  java
  • hud 1785 畅通工程

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    int par[103];
    struct Island
    {
        int x,y;
    } isl[103];
    struct Node
    {
        int isl1,isl2;
        double dis;
    } rode[100*99/2];
    int find(int a)
    {
        if(a!=par[a])
        {
            par[a]=find(par[a]);
        }
        return par[a];
    }
    void build(int a,int b)
    {
        int fa=find(a);
        int fb=find(b);
        if(fa!=fb)
        {
            par[fb]=fa;
        }
    }
    bool cmp(Node a,Node b)
    {
        return a.dis<b.dis;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int c;
            scanf("%d",&c);
            if(c == 1 || c == 0) {printf("oh!
    "); continue;}
            for(int i=0; i<c; i++)
                par[i]=i;
            int num=c*(c-1)/2;
            for(int i=0; i<c; i++)
            {
                scanf("%d%d",&isl[i].x,&isl[i].y);
            }
            int t=0;
            for(int i=0; i<c; i++)
            {
                for(int j=i+1; j<c; j++)
                {
                    double mid=double((isl[i].x-isl[j].x)*(isl[i].x-isl[j].x)+(isl[i].y-isl[j].y)*(isl[i].y-isl[j].y));
                    if(mid<100||mid>1000000) continue;
                    else
                    {
                        rode[t].isl1=i;
                        rode[t].isl2=j;
                        rode[t].dis=sqrt(mid);
                        t++;
                    }
                }
            }
            //cout<<t<<"  num  " <<num;
            double ans=0.0;
            int cnt=0;
            sort(rode,rode+t,cmp);
            for(int i=0;i<t;i++)
            {
                if(find(rode[i].isl1)!=find(rode[i].isl2))
                {
                    ans+=rode[i].dis;
                    build(rode[i].isl1,rode[i].isl2);
                    cnt++;
                }
            }
            ans*=100;
            if(cnt!=c-1)  printf("oh!
    ");
            else printf("%.1lf
    ",ans);
        }
        return 0;
    }
    View Code
     
     
    畅通工程再续
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
     

    Input

    输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。 
    每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。 
     

    Output

    每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
     

    Sample Input

    2 2 10 10 20 20 3 1 1 2 2 1000 1000
     

    Sample Output

    1414.2 oh!
     
     
    简单的最小生成树,先把所有的可能的桥找出来,然后最小生成树。
  • 相关阅读:
    SGU 187 Twist and whirl
    伸展树---初步学习
    poj 2503 Babelfish
    sublime 3 phpfmt配置(大括号对齐)
    Linux Shell 错误: $' ': command not found错误解决
    redis 使用场景
    wireshake tcp 三次握手详解
    ip地址和子网掩码
    phpstorm 远程调式 php
    win10,ubuntu时间不对问题
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5334110.html
Copyright © 2011-2022 走看看