您的位置:首页 > 其它

POJ 2349 Arctic Network (最小生成树)

2017-08-04 11:56 399 查看
The Department of National Defence (DND) wishes to connect several northern outposts
4000
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

题目描述简直一片混乱,最后自己根据实际情况猜测的题意,有s个卫星通信设备(注意:不是卫星!)有p个前哨站,现在有两种方式可以实现通信,一种是两个前哨站之间用卫星通信设备通信,这一种没有距离的限制,第二种是通过无线广播通信,这一种有距离限制,最远为d。d越大建造成本越高,为了维修采购方便,要求所有前哨站的无线广播设备都是相同的,即d是相同的,求一个最小的d,保证每个前哨站连通。

先求出最小生成树,边由小到大排序,让边权值大的用卫星设备通信,这样s个卫星设备就代替了s-1条边,之后再找最大的边就是答案。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;

const int M = 505;
const int INF = 0x3f3f3f3f;
struct Node
{
double x, y;
}outpost[M];
struct Edge
{
int u, v;
double w;
};
vector<Edge> edge;
bool cmp(Edge a, Edge b)
{
return a.w < b.w;
}
vector<double> tree;
int pre[M];
int Find(int x)
{
int r = x;
while(pre[r]!=r)
r = pre[r];
int i = x, j;
while(pre[i]!=r)
{
j = pre[i];
pre[i] = r;
i = j;
}
return r;
}
int main()
{
int s, p, t;
Edge tmp;
double dis;
scanf("%d", &t);
while(t--)
{
tree.clear();
edge.clear();
scanf("%d%d", &s, &p);
for(int i=1;i<=p;i++)
{
scanf("%lf%lf", &outpost[i].x, &outpost[i].y);
pre[i] = i;
}
for(int i=1;i<=p;i++)
for(int j=i+1;j<=p;j++)
{
dis = sqrt((outpost[i].x-outpost[j].x)*(outpost[i].x-outpost[j].x) + (outpost[i].y-outpost[j].y)*(outpost[i].y-outpost[j].y));
tmp.u = i;
tmp.v = j;
tmp.w = dis;
edge.push_back(tmp);
}
sort(edge.begin(), edge.end(), cmp);
int cnt = 0;
vector<Edge>::iterator it;
for(it=edge.begin(); it!=edge.end();it++)
{
if(Find(it->u)!=Find(it->v))
{
tree.push_back(it->w);
cnt++;
pre[Find(it->u)] = Find(it->v);
}
if(cnt == p-1)
break;
}
printf("%.2f\n", tree[p-1-s]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: