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
题意:
有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;
}