• POJ 2349 Arctic Network (最小生成树)


    Arctic Network
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Appoint description: 

    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.

    Sample Input

    1
    2 4
    0 100
    0 300
    0 600
    150 750
    

    Sample Output

    212.13



    即求最小生成树中倒数第S + 1大的边,注意不能一开始就把最大的S条边合并,因为这些边不一定能取到,要在kruskal里处理。
      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 = 505;
     16 int    FATHER[SIZE];
     17 int    N,M,NUM;
     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 bool    comp(const Node &,const Node &);
     33 double    dis(int,int,int,int);
     34 double    kruskal(void);
     35 int    main(void)
     36 {
     37     int    t;
     38     
     39     scanf("%d",&t);
     40     while(t --)
     41     {
     42         scanf("%d%d",&M,&N);
     43         ini();
     44         for(int i = 1;i <= N;i ++)
     45             scanf("%d%d",&TEMP[i].x,&TEMP[i].y);
     46         for(int i = 1;i <= N;i ++)
     47             for(int j = i + 1;j <= N;j ++)
     48             {
     49                 G[NUM].from = i;
     50                 G[NUM].to = j;
     51                 G[NUM].cost = dis(TEMP[i].x,TEMP[j].x,TEMP[i].y,TEMP[j].y);
     52                 NUM ++;
     53             }
     54         sort(G,G + NUM,comp);
     55         printf("%.2f
    ",kruskal());
     56     }
     57 
     58     return    0;
     59 }
     60 
     61 void    ini(void)
     62 {
     63     NUM = 0;
     64     for(int i = 0;i <= N;i ++)
     65         FATHER[i] = i;
     66 }
     67 
     68 int    find_father(int n)
     69 {
     70     if(n == FATHER[n])
     71         return    n;
     72     return    FATHER[n] = find_father(FATHER[n]);
     73 }
     74 
     75 void    unite(int x,int y)
     76 {
     77     x = find_father(x);
     78     y = find_father(y);
     79 
     80     if(x == y)
     81         return    ;
     82     FATHER[x] = y;
     83 }
     84 
     85 bool    same(int x,int y)
     86 {
     87     return    find_father(x) == find_father(y);
     88 }
     89 
     90 bool    comp(const Node & a,const Node & b)
     91 {
     92     return    a.cost < b.cost;
     93 }
     94 
     95 double    dis(int x_1,int x_2,int y_1,int y_2)
     96 {
     97     return    pow(x_1 - x_2,2) + pow(y_1 - y_2,2);
     98 }
     99 
    100 double    kruskal(void)
    101 {
    102     int    count = 0;
    103     double    ans = 0,temp;
    104     double    box[SIZE];
    105 
    106     for(int i = 0;i < NUM;i ++)
    107         if(!same(G[i].from,G[i].to))
    108         {
    109             unite(G[i].from,G[i].to);
    110             box[count ++] = sqrt(G[i].cost);
    111             if(count == N - 1)
    112                 break;
    113         }
    114     sort(box,box + count);
    115     if(N - 1 - M >= 0)
    116         ans = box[N - 1 - M];
    117     else
    118         ans = 0;
    119 
    120     return    ans;
    121 }
  • 相关阅读:
    Blazor client-side + webapi (.net core 3.1) 添加jwt验证流程(非host)第一步
    移动端web框架候选
    Blazor初体验之寻找存储client-side jwt token的方法
    python学习记录(持续更新)--最最最基础的一部分(方法,异常处理,注释,类)
    python学习记录(持续更新)--最最最基础的一部分(条件分支、循环、数组矩阵、元组)
    python学习记录(持续更新)--最最最基础的一部分(变量、字符串、运算符、常见函数)
    C#后台异步消息队列实现
    Quartz.Net 删除一个Job
    授权认证(IdentityServer4)
    文件同步
  • 原文地址:https://www.cnblogs.com/xz816111/p/4547850.html
Copyright © 2020-2023  润新知