您的位置:首页 > 其它

最小生成树(prim)--poj2349

2013-08-19 21:37 316 查看
Language:
Default

Arctic Network

Time Limit: 2000MS

Memory Limit: 65536K

Total 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
1
2 4
0 100
0 300
0 600
150 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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: