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
  • 相关阅读:
    Python 使用gevent下载图片案例
    Python 使用gevent实现多任务
    Python 生成器
    Python 自己实现可迭代对象
    Python 遍历文件夹清理磁盘案例
    Python 多进程拷贝文件夹案例
    Python 多进程编程
    Python 多线程编程
    Python TCP通信之仿文件下载
    Python UDP 通信
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7285018.html
Copyright © 2011-2022 走看看