最小生成树(prim)--poj2349

来源:互联网 发布:消费主义 知乎 编辑:程序博客网 时间:2024/06/06 09:51
Language:
Arctic Network
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7917 Accepted: 2638

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

12 40 1000 3000 600150 750

Sample Output

212.13题意:有两种不同的通信技术,有卫星通信的两个城市之间可以任意联络,但用无线电通信的城市只能和距离不超过D的城市联系。无线电的能力越高(即传输距离D越大),花费就越大。已知无线电的数目m,让求最小的D。思路:prim求最小生成树,然后对dis数组排序,去掉前s大的数,然后找出最大的D。代码如下:
#include<iostream>#include<cmath>#include<cstring>#include<algorithm>#include<cstdio>using namespace std;const int inf=1000000000;struct A{    int x,y;} biao[550];double dis[550];double map[550][550];int vis[550];int s,p;double juli(A a,A b){    return sqrt(double((a.x-b.x)*(a.x-b.x))+double((a.y-b.y)*(a.y-b.y)));}void prim(){    double max1;    for(int i=1; i<=p; i++)    {        dis[i]=map[1][i];    }    vis[1]=1;    int w;    for(int i=1; i<p; i++)    {        max1=1000000000;        for(int j=1; j<=p; j++)            if(!vis[j]&&dis[j]<max1)            {                max1=dis[j];                w=j;            }        vis[w]=1;        for(int j=1; j<=p; j++)        {            if(!vis[j]&&dis[j]>map[j][w])                dis[j]=map[j][w];        }    }    sort(dis+1,dis+p+1);    max1=0;    for(int i=2; i<=p-s+1; i++)        if(dis[i]>max1)            max1=dis[i];    printf("%.2lf\n",max1);}int main(){    //freopen("in.txt","r",stdin);    int t;    cin>>t;    while(t--)    {        cin>>s>>p;        for(int i=1; i<=p; i++)        {            cin>>biao[i].x>>biao[i].y;        }        memset(map,inf,sizeof(map));        for(int i=1; i<=p; i++)            for(int j=1+i; j<=p; j++)            {                map[i][j]=map[j][i]=juli(biao[i],biao[j]);            }        memset(vis,0,sizeof(vis));        prim();    }    return 0;}


原创粉丝点击