poj 2349 Arctic Network

Arctic Network

Time Limit: 2000MS

Memory Limit: 65536K

Total Submissions: 5764

Accepted: 2007

题目链接:http://poj.org/problem?id=2349

Description

The Department of NationalDefence (DND) wishes to connect several northern outposts by a wirelessnetwork. Two different communication technologies are to be used inestablishing the network: every outpost will have a radio transceiver and someoutposts 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 radioonly if the distance between them does not exceed D, which depends of the powerof the transceivers. Higher power yields higher D but costs more. Due topurchasing and maintenance considerations, the transceivers at the outpostsmust be identical; that is, the value of D is the same for every pair ofoutposts.

Your job is to determine the minimum D required for the transceivers. Theremust be at least one communication path (direct or indirect) between every pairof outposts.

Input

The first line of inputcontains N, the number of test cases. The first line of each test case contains1 <= S <= 100, the number of satellite channels, and S < P <= 500,the number of outposts. P lines follow, giving the (x,y) coordinates of eachoutpost in km (coordinates are integers between 0 and 10,000).

Output

For each case, output shouldconsist 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

Source

Waterloolocal 2002.09.28

 

题意:

         m个坐标点,没两个点之间通信,如果某两个点有卫星频道,,那么这两个点可以无限距离通信,如果没有,那么只能在一定范围D内通信,问D的最大值是多少

解题思路:

         m各坐标点抽象成一张完全图图,再用prim算法计算其最小生成树,记录最小生成树每条边的权值,在对边进行从大到小排序,在取该数组中的n个值即可

 

代码:

#include <iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 1003
#define VALUE 999999999
using namespace std;


struct distinct
{
    int x;
    int y;
};

double g[MAX][MAX];
double minCost[MAX];
int visited[MAX];
double arr[MAX];
//求两点之间的距离
double dis(int x1,int y1,int x2,int y2)
{
    return sqrt((x2-x1)*(x2-x1)*1.0+(y2-y1)*(y2-y1)*1.0);
}

//求最小生成树
void prim(int n)
{
    int i;
	int temp=0;
    for(i=0;i<n;i++)
    {
        visited[i]=0;
        minCost[i]=g[0][i];
		arr[i]=0;
    }
    minCost[0]=0;
    while(true)
    {
        int t=-1;
        for(i=0;i<n;i++)
        {
            if(visited[i]==0 && (t==-1 || minCost[i]<minCost[t]))
            {
                t=i;
            }
        }
        if(t==-1)
            break;
        visited[t]=1;
		arr[temp]=minCost[t];//保存每一条最短路径
		temp++;
        //printf("%d\t",minCost[t]);
        for(i=0;i<n;i++)
        {
            if(minCost[i]>g[i][t] && g[i][t]!=0)
            {
                minCost[i]=g[i][t];
            }
        }
    }
}

bool cmp(double i,double j)    //比较函数。
{
	return i>j;
}


int main()
{
    int ts;
    int i,j;
    scanf("%d",&ts);
    while(ts--)
    {
        memset(g,0,sizeof(g));
        int n,m;
        distinct d[MAX];
        memset(d,0,sizeof(d));
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&d[i].x,&d[i].y);
            for(j=0;j<i;j++)
            {//建立无向完全图
                g[i][j]=dis(d[i].x,d[i].y,d[j].x,d[j].y);
                g[j][i]=g[i][j];
            }
        }
       prim(m);

	   //对数组minCost进行从大到小排序
	   sort(arr,arr+m,cmp);



	   if(n==0)
	   {
	       printf("%.2lf\n",arr[0]);
	   }
	   else
		   printf("%.2lf\n",arr[n-1]);
    }
    return 0;
}


 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值