• 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
  • 相关阅读:
    java内存模型
    类、对象和接口
    Python--数据存储:pickle模块的使用讲解
    Python--常用的内置函数
    Python--迭代器和生成器的定义和案例
    Python--作业2--对员工信息文件,实现增删改查操作
    Python--文本基本操作
    Python--字符串基本操作
    Python--字典基本操作
    Python--作业1--购物车程序
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7285018.html
Copyright © 2020-2023  润新知