zoukankan      html  css  js  c++  java
  • poj 2349 Arctic Network

    Description

    The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel. 
    Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts. 

    Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts. 

    Input

    The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000). 

    Output

    For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
     
    Wa,服了,要不是看了discuss我一辈子也写不出来
    输出用%0.2f就ac,用%0.2lf就wa。。。
    -----
    正题:
      就是让你求最小生成树中的第s大的边,用kruskal感觉方便很多
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    
    int T,s,p;
    struct node{
      double x,y;
    };
    struct node point[505];
    struct edge{
      double len;
      int a,b;
    };
    struct edge e[500001];
    double res[1001];
    int father[505];
    int total;
    int Getf(int x){
      if (father[x] == x) return x;
      father[x] = Getf(father[x]);
      return father[x];
    }
    
    bool cmp(struct edge e1,struct edge e2){
      return (e1.len < e2.len);
    }
    bool cmp2(double a,double b){
      return (a > b);
    }
    
    void kruskal(int p){
      int num = 0;
      memset(res,0.0,sizeof(res));
      for (int i=1;i<=total;i++){
        int a = e[i].a,b = e[i].b;
        int fa = Getf(a),fb = Getf(b);
        if (fa != fb){
          father[fa] = fb;
          num ++;
          res[num] = e[i].len;
          if (num == p-1) break;
        }
      }
      sort(res+1,res+num+1,cmp2);
      printf("%0.2f
    ",res[s]);
    }
    double CalcDist(int a,int b){
      double ax = point[a].x,ay = point[a].y;
      double bx = point[b].x,by = point[b].y;
      return sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by));
    }
    
    
    
    int main(){
      // freopen("test.in","r",stdin);
      scanf("%d",&T);
      for (int times=1;times<=T;times++){
        scanf("%d %d",&s,&p);
        for (int i=1;i<=p;i++){
          scanf("%lf%lf",&point[i].x,&point[i].y);
          father[i] = i;
        }
        total = 0;
        for (int i=1;i<p;i++){
          for (int j=i+1;j<=p;j++){
            total ++;
            e[total].len = CalcDist(i,j);
            e[total].a = i; e[total].b = j;
          }
        }
        sort(e+1,e+total+1,cmp);
        kruskal(p);
    
      }
      return 0;
    }
    View Code
  • 相关阅读:
    ZH奶酪:JavaScript中的JSON.stringify() and JSON.parse()
    ZH奶酪:PHP中添加HTML代码的三种方法
    ZH奶酪:Ionic中(弹出式窗口)的$ionicModal使用方法
    ZH奶酪:JavaScript清空数组的三种方法
    ZH奶酪:AngularJS判断checkbox/复选框是否选中并实时显示
    ZH奶酪:JavaScript调用AngularJS的函数/$scope/变量
    ZH奶酪:PHP如何判断提交表单中多个复选框是否选中?
    ZH奶酪:PHP解析URL及parse_url、parse_str、explode、implode函数说明
    ZH奶酪:Ubuntu14.04 安装Android SDK(SDK tools only)
    ZH奶酪:Word断字功能不可用。请运行Microsoft Office安装程序安装该功能,然后重试。
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7285018.html
Copyright © 2011-2022 走看看