• TZOJ 2415 Arctic Network(最小生成树第k小边)


    描述

    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.

    输入

    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).

    输出

    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.

    样例输入

    1
    2 4
    0 100
    0 300
    0 600
    150 750

    样例输出

    212.13

    题意

    二维平面给你p个点的图,问你形成最多s个连通块使所有边的最大值最小,并输出

    题解

    很容易想到最小生成树,每连一条边就少一个连通块,当我们最后形成s个连通块时找出所有边的最大值就是最小值

    Kruskal算法恰好是按边大小排序,所以答案就是要我们从大到小输出第cnt-s+1条边

    代码

     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 int s,p,F[505];
     7 struct edge
     8 {
     9     int u,v;
    10     double w;
    11 }edges[260000];
    12 bool cmp(edge a,edge b)
    13 {
    14     return a.w<b.w;
    15 }
    16 int Find(int x)
    17 {
    18     return F[x]==x?x:F[x]=Find(F[x]);
    19 }
    20 int tot=0;
    21 void Kruskal()
    22 {
    23     for(int i=0;i<=p;i++)F[i]=i;
    24     sort(edges,edges+tot,cmp);
    25     int cnt=0;
    26     double D[505];
    27     for(int i=0;i<tot;i++)
    28     {
    29         int fu=Find(edges[i].u);
    30         int fv=Find(edges[i].v);
    31         double fw=edges[i].w;
    32         if(fu!=fv)
    33         {
    34             cnt++;
    35             F[fu]=fv;
    36             D[cnt]=fw;
    37             if(cnt==p-1)break;
    38         }
    39     }
    40     if(s>=p)printf("0.00
    ");
    41     else printf("%.2f
    ",D[cnt-s+1]);
    42 }
    43 int main()
    44 {
    45     int t;
    46     scanf("%d",&t);
    47     while(t--)
    48     {
    49         tot=0;
    50         int x[505],y[505];
    51         scanf("%d%d",&s,&p);
    52         for(int i=1;i<=p;i++)
    53         {
    54             scanf("%d%d",&x[i],&y[i]);
    55             for(int j=1;j<=i;j++)
    56             {
    57                 double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    58                 edges[tot].u=i;
    59                 edges[tot].v=j;
    60                 edges[tot++].w=dis;
    61             }
    62         }
    63 
    64         Kruskal();
    65     }
    66     return 0;
    67 }
  • 相关阅读:
    java List转换为字符串并加入分隔符的一些方法总结
    jackson 实体转json 为NULL或者为空不参加序列化
    马云告别演讲
    Linux chmod命令
    Linux执行shell脚本的方法
    2019第36周日
    2019第36周六
    eclipse中的maven插件
    SpringBoot要点之使用Actuator监控
    eclipse隐藏的列编辑
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9334552.html
Copyright © 2020-2023  润新知