• HDU 1875 畅通工程再续 (最小生成树)


    畅通工程再续

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 17913    Accepted Submission(s): 5593


    Problem 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!
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <string>
      4 #include <queue>
      5 #include <vector>
      6 #include <map>
      7 #include <algorithm>
      8 #include <cstring>
      9 #include <cctype>
     10 #include <cstdlib>
     11 #include <cmath>
     12 #include <ctime>
     13 using    namespace    std;
     14 
     15 const    int    SIZE = 105;
     16 int    FATHER[SIZE],N,M,NUM;
     17 int    MAP[SIZE][SIZE];
     18 struct    Node
     19 {
     20     int    from,to;
     21     double    cost;
     22 }G[SIZE * SIZE];
     23 struct
     24 {
     25     int    x,y;
     26 }TEMP[SIZE];
     27 
     28 void    ini(void);
     29 int    find_father(int);
     30 void    unite(int,int);
     31 bool    same(int,int);
     32 void    kruskal(void);
     33 bool    comp(const Node &,const Node &);
     34 double    dis(int,int,int,int);
     35 int    main(void)
     36 {
     37     int    t;
     38     double    temp;
     39 
     40     scanf("%d",&t);
     41     while(t --)
     42     {
     43         scanf("%d",&N);
     44         ini();
     45         for(int i = 1;i <= N;i ++)
     46             scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
     47         for(int i = 1;i <= N;i ++)
     48             for(int j = i + 1;j <= N;j ++)
     49             {
     50                 temp = sqrt(dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y));
     51                 if(temp >= 10 && temp <= 1000)
     52                 {
     53                     G[NUM].from = i;
     54                     G[NUM].to = j;
     55                     G[NUM].cost = temp * 100;
     56                     NUM ++;
     57                 }
     58             }
     59         sort(G,G + NUM,comp);
     60         kruskal();
     61     }
     62 
     63     return 0;
     64 }
     65 
     66 void    ini(void)
     67 {
     68     NUM = 0;
     69     for(int i = 1;i <= N;i ++)
     70         FATHER[i] = i;
     71 }
     72 
     73 int    find_father(int n)
     74 {
     75     if(FATHER[n] == n)
     76         return    n;
     77     return    FATHER[n] = find_father(FATHER[n]);
     78 }
     79 
     80 void    unite(int x,int y)
     81 {
     82     x = find_father(x);
     83     y = find_father(y);
     84 
     85     if(x == y)
     86         return    ;
     87     FATHER[x] = y;
     88 }
     89 
     90 bool    same(int x,int y)
     91 {
     92     return    find_father(x) == find_father(y);
     93 }
     94 
     95 bool    comp(const Node & a,const Node & b)
     96 {
     97     return    a.cost < b.cost;
     98 }
     99 
    100 void    kruskal(void)
    101 {
    102     int    count = 0;
    103     double    ans = 0;
    104 
    105     for(int i = 0;i < NUM;i ++)
    106         if(!same(G[i].from,G[i].to))
    107         {
    108             unite(G[i].from,G[i].to);
    109             count ++;
    110             ans += G[i].cost;
    111             if(count == N - 1)
    112                 break;
    113         }
    114     if(count == N - 1)
    115         printf("%.1f
    ",ans);
    116     else
    117         puts("oh!");
    118 }
    119 
    120 double    dis(int x_1,int y_1,int x_2,int y_2)
    121 {
    122     return    pow(x_1 - x_2,2) + pow(y_1 - y_2,2);
    123 }
  • 相关阅读:
    数组模拟链表
    高精度模板
    利用二分法求三次方根
    AcWing 789.数的范围
    二进制中1的个数
    AcWing 787.归并排序
    微信小程序form表单的bindsubmit提交没有效果
    本地项目如果上传到GitHub上
    微信小程序生成随机数
    CSS3 Filter的十种特效
  • 原文地址:https://www.cnblogs.com/xz816111/p/4549533.html
Copyright © 2020-2023  润新知