zoukankan      html  css  js  c++  java
  • HDU 1875 畅通project再续 (最小生成树 水)


    Problem Description
    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其它的小岛时都要通过划小船来实现。

    如今政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米。也不能大于1000米。

    当然。为了节省资金。仅仅要求实现随意2个小岛之间有路通就可以。

    当中桥的价格为 100元/米。

     


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


    Output
    每组输入数据输出一行。代表建桥的最小花费,结果保留一位小数。假设无法实现project以达到所有畅通,输出”oh!”.
     


    Sample Input
    2 2 10 10 20 20 3 1 1 2 2 1000 1000
     


    Sample Output
    1414.2 oh!


    总共最多有100个节点。那么边最大100*100/2,把全部 符合题意 的边都求出来,然后就是最小生成树了。


    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #define lson o<<1, l, m
    #define rson o<<1|1, m+1, r
    using namespace std;
    typedef long long LL;
    const int maxn = 105;
    const int mod = 1000000007;
    int n, t, fa[105];
    double q;
    struct C1{
        int st, en;
        double v;
    }ed[6000];
    struct C2 {
        int x, y;
    }in[105];
    double getdis(double x1, double y1, double x2, double y2) {
        return sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
    }
    bool cmp (C1 x, C1 y) {
        return x.v < y.v;
    }
    int Find (int x) {
        return x == fa[x] ? x : x = Find(fa[x]);
    }
    int main()
    {
        scanf("%d", &t);
        while(t--) {
            int n;
            scanf("%d", &n);
            for(int i = 0; i < n; i++) scanf("%d%d", &in[i].x, &in[i].y);
            int k = 0;
            for(int i = 0; i < n-1; i++)
                for(int j = i+1; j < n; j++) {
                    double tmp = getdis(in[i].x, in[i].y, in[j].x, in[j].y);
                    if(tmp >= 10 && tmp <= 1000) {
                        ed[k].v = tmp*100;
                        ed[k].st = i;
                        ed[k++].en = j;
                    }
                }
            sort(ed, ed+k, cmp);
            for(int i = 0; i <= 100; i++) fa[i] = i;
            double sum = 0;
            int cnt = 0;
            for(int i = 0; i <k; i++) {
                int pres = Find(ed[i].st), pree = Find(ed[i].en);
                if(pres != pree) {
                    fa[pres] = pree;
                    sum += ed[i].v;
                    cnt++;
                    if(cnt == n-1) break;
                }
            }
            if(cnt == n-1) printf("%.1lf
    ", sum);
            else printf("oh!
    ");
        }
    
        return 0;
    }
    


    
  • 相关阅读:
    165. Compare Version Numbers
    164. Maximum Gap
    3、桶排序
    162. Find Peak Element
    160. Intersection of Two Linked Lists
    155. Min Stack
    154. Find Minimum in Rotated Sorted Array II
    153. Find Minimum in Rotated Sorted Array
    Linux/Unix系统编程手册 第二章:基本概念
    Linux/Unix系统编程手册 第一章:历史和标准
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6820449.html
Copyright © 2011-2022 走看看