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


    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
    
    题目意思:有P个哨所和S个卫星,要建立所有哨所之间的联系。有卫星的两个哨所之间可以任意通信;否则一个哨所只能和距离它小于等于D的哨所通信。给出P个哨所的坐标,求D的最小值。
    解题思路:为了使这P个哨所建立起联系,我们需要树状图,所以需要使用最小生成树的算法,将所有点建立起联系。但是为了所得到的D最小,对于最小生成树种那些边长较长的边,我们可以使用卫星进行联系,有S个卫星,这样就将最小生成树划分为了S个割集,割集之间使用卫星联系,割集之内使用无线联系,求割集内的点之间的最大值即为最小的D。

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<cmath>
      4 #include<algorithm>
      5 #define maxs 550
      6 using namespace std;
      7 int pre[maxs];
      8 int m,k;
      9 int s,p;
     10 double dis[maxs*maxs];
     11 struct node///各个哨所的坐标
     12 {
     13     int x;
     14     int y;
     15 } a[maxs];
     16 struct edge///哨所之间联系构成边
     17 {
     18     int u;
     19     int v;
     20     double w;
     21 } e[maxs*maxs];
     22 bool my_cmp(edge a,edge b)
     23 {
     24     return a.w<b.w;
     25 }
     26 bool my_cmp2(double a,double b)
     27 {
     28     return a>b;
     29 }
     30 double Getdis(node a,node b)///获得各个边的距离
     31 {
     32     return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
     33 }
     34 int Find(int x)
     35 {
     36     if(x!=pre[x])
     37     {
     38         pre[x]=Find(pre[x]);
     39     }
     40     return pre[x];
     41 }
     42 void Union(int a,int b)
     43 {
     44     int x=Find(a);
     45     int y=Find(b);
     46     if(x>y)
     47     {
     48         pre[x]=y;
     49     }
     50     else
     51     {
     52         pre[y]=x;
     53     }
     54 }
     55 void Kruskal()
     56 {
     57     memset(dis,0.0,sizeof(dis));
     58     int i,counts;
     59     counts=0;
     60     for(i=1; i<=m; i++)
     61     {
     62         int fx=Find(e[i].u);
     63         int fy=Find(e[i].v);
     64         if(fx!=fy)
     65         {
     66             pre[fx]=fy;
     67             counts++;
     68             dis[counts]=e[i].w;
     69             if(counts==p-1)///p个点,p-1条边
     70             {
     71                 break;
     72             }
     73         }
     74     }
     75     sort(dis+1,dis+counts+1,my_cmp2);
     76     printf("%.2f
    ",dis[s]);///最小的D为所有边中排除前S大后最大的那一个
     77 }
     78 int main()
     79 {
     80     int t,i,j;
     81     scanf("%d",&t);
     82     while(t--)
     83     {
     84         scanf("%d%d",&s,&p);
     85         for(i=1; i<=p; i++)
     86         {
     87             pre[i]=i;
     88         }
     89         m=0;
     90         for(i=1; i<=p; i++)
     91         {
     92             scanf("%d%d",&a[i].x,&a[i].y);
     93         }
     94         for(i=1; i<=p; i++)
     95         {
     96             for(j=i+1; j<=p; j++)
     97             {
     98                 m++;
     99                 e[m].u=i;
    100                 e[m].v=j;
    101                 e[m].w=Getdis(a[i],a[j]);
    102             }
    103         }
    104         sort(e+1,e+m+1,my_cmp);
    105         Kruskal();
    106     }
    107     return 0;
    108 }
     
  • 相关阅读:
    Chrome 使用 Evernote 插件
    【MySQL】ON DUPLICATE KEY UPDATE
    【MySQL】Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and ...
    libevent API
    LLServer--》对LevelDB的应用
    Linux信号signal处理机制
    守护进程
    使用 libevent 和 libev 提高网络应用性能(IBM)
    Libev和LibEvent
    利用TokyoTyrant构建兼容Memcached协议、支持故障转移、高并发的分布式Key-value持久存储系统(转)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9832569.html
Copyright © 2020-2023  润新知