• [poj 2349]Arctic Network


    Arctic Network
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 19978   Accepted: 6226

    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
    

    题目大意
    先搞一个最小生成树,把每条边的距离存起来,选择需要使用限制距离的那东西的最大距离

    最小生成树。感觉prim好写一点。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cmath>
     4 #include<cstdio>
     5 #include<cstdlib>
     6 #include<cstring>
     7 
     8 using namespace std;
     9 
    10 int read()
    11 {
    12     char ch=getchar();
    13     int x=0,f=1;
    14     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    15     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    16     return x*f;
    17 }
    18 int n,t,s,tmp;
    19 int x[510],y[510];
    20 double map[510][510],r[250000],ans[510],low[510];
    21 void prim()
    22 {
    23     int pos=1,tmpos,tmp2=0;
    24     bool vis[501];
    25     memset(vis,0,sizeof(vis));
    26     vis[1]=1;
    27     for(int i=1;i<=s;i++)low[i]=map[pos][i];
    28     for(int i=1;i<s;i++)
    29     {
    30         double minn=30001;
    31         for(int j=1;j<=s;j++)
    32         {
    33             if(vis[j]==0 && low[j]<minn)
    34             {
    35                 minn=low[j];
    36                 tmpos=j;
    37             }
    38         }
    39         pos=tmpos;
    40         vis[pos]=1;
    41         ans[tmp2]=minn;
    42         tmp2++;
    43         for(int j=1;j<=s;j++)
    44         {
    45             if(vis[j]==0 && map[pos][j]<low[j])low[j]=map[pos][j];
    46         }
    47     }
    48 }
    49 int main()
    50 {
    51     n=read();
    52     for(int i=1;i<=n;i++)
    53     {
    54         t=read();s=read();
    55         for(int j=1;j<=s;j++)
    56         {
    57             x[j]=read();
    58             y[j]=read();
    59             for(int k=1;k<j;k++)
    60             {
    61                 map[j][k]=sqrt((double)(x[j]-x[k])*(double)(x[j]-x[k])+(double)(y[j]-y[k])*(double)(y[j]-y[k]));
    62                 map[k][j]=map[j][k];
    63             }
    64         }
    65         prim();
    66         sort(ans,ans+s-1);
    67         printf("%.2f
    ",ans[s-t-1]);
    68     }
    69     system("pause");
    70     return 0;
    71 }
    View Code
    
    
    
     
  • 相关阅读:
    排序算法分析
    图论算法小结
    A*算法
    分支界限法的应用
    图的搜索策略
    最大二分匹配
    C++学习笔记(1)
    vscode简单c语言多文件编译
    c语言变量大小
    十大排序算法总结
  • 原文地址:https://www.cnblogs.com/taojy/p/7168659.html
Copyright © 2020-2023  润新知