#include

HDU 最先生成树 第n大的边

Problem L

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 85   Accepted Submission(s) : 22
Problem 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
国防部(DND)希望通过无线网络连接几个北部前哨站。建立网络需要使用两种不同的通信技术:每个前哨都有一个无线电收发器,一些前哨基站还有一个卫星通道。
任何两个卫星频道的前哨都可以通过卫星进行通信,无论他们的位置如何。否则,只有两个前哨之间的距离不超过D,才能通过无线电进行通信,这取决于收发器的功率。功率越高,功率越高,但成本也越高。由于购买和维护考虑,前哨收发器必须相同;也就是说,每个前哨对的D值都是一样的。

您的工作是确定收发器所需的最小D值。每两个前哨之间必须至少有一条通信路径(直接或间接)。
 

输入
第一行输入包含N,即测试用例的数量。每个测试案例的第一行包含1 <= S <= 100,卫星频道数量和S <P <= 500,即前哨数量。 P行,给出每个前哨(km,坐标是0到10,000之间的整数)的(x,y)坐标。
 

产量
对于每种情况,输出应由一条线路组成,以提供连接网络所需的最小D值。输出应指定为2个小数点。
 
最小生成树将所有点连接起来且边尽可能小,有s个卫星,则可以去掉s-1个树中的最大边,输出现存的最大边
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>

using namespace std ; 

#define maxn 1100
#define INF 999999999

double map[maxn][maxn] , dis[maxn] ; 
bool visit[maxn] ; 

int t , n , m ; 

double x[maxn] , y[maxn] ; 

void prime(int s){
    int u ; 
    double min ; 
    memset(visit , false , sizeof(visit)) ; 
    visit[s] = true ; 

    for(int i=1 ; i<=m ; i++){
        dis[i] = map[s][i] ; 
    }
    dis[s] = 0 ; 
    for(int i=1 ; i<=m ; i++){
        u = s ; 
        min = INF ; 
        for(int j=1 ; j<=m ; j++){
            if(!visit[j] && dis[j] < min){
                min = dis[j] ; 
                u = j ; 
            }
        }
        visit[u] = true ; 
        for(int j=1 ; j<=m ; j++){
            if(!visit[j] && dis[j] > map[u][j]){
                dis[j] = map[u][j] ; 
            }
        }
    }
    sort(dis+1 , dis+1+m ) ; 
    printf("%.2f\n" , dis[m-n+1]) ; 
    return;
}

int main(){

    cin >> t ; 
    while(t--){
        cin >> n >> m ; 
        for(int i=1 ; i<=m ; i++){
            cin >> x[i] >> y[i] ; 
        }
        for(int i=1 ; i<=m ; i++){
            for(int j=1 ; j<i ; j++){
                map[i][j] = map[j][i] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])) ; 
            }
            map[i][i] = INF ; 
        }
        prime(1) ; 
    }

    return 0 ; 
}

 

posted @ 2018-05-20 22:34  0一叶0知秋0  阅读(161)  评论(0编辑  收藏  举报