码迷,mamicode.com
首页 > Web开发 > 详细

POJ2349--Arctic Network

时间:2015-01-25 15:19:43      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10955   Accepted: 3592

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

Source

Waterloo local 2002.09.28

方法1:二分D+并查集,如果连通块数目小于等于s,则D可能可以更小,不然更大,poj上耗时大概800多ms

/*************************************************************************
    > File Name: POJ2349.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月25日 星期日 13时33分36秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int father[555];
struct node
{
	int x, y;
}point[555];

int find (int x)
{
	if (father[x] == -1)
	{
		return x;
	}
	return father[x] = find (father[x]);
}

double dist(node a, node b)
{
	return sqrt ((double)((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}

int main ()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int s, n;
		scanf("%d%d", &s, &n);
		double l = 0;
		double r = 20000;
		double mid;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &point[i].x, &point[i].y);
		}
		double ret;
		while (l + 1e-6 < r)
		{
			int ans = n;
			memset (father, -1, sizeof(father));
			mid = (l + r) / 2;
			for (int i = 1; i <= n; ++i)
			{
				for (int j = i + 1; j <= n; ++j)
				{
					if (dist(point[i], point[j]) + 1e-6 <= mid)
					{
						int u = find (i);
						int v = find (j);
						if (u != v)
						{
							father[u] = v;
							--ans;
						}
					}
				}
			}
			if (ans <= s)
			{
				r = mid;
				ret = mid;
			}
			else
			{
				l = mid;
			}
		}
		printf("%.2f\n", ret);
	}
	return 0;
}

法二:MST,标记出加入到MST中的边,去掉最大的s-1条,剩下的边里最大的就是D,耗时大概几十ms


/*************************************************************************
    > File Name: poj2349.cpp
    > Author: ALex
    > Mail: 405045132@qq.com 
    > Created Time: 2015年01月25日 星期日 13时08分14秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

double mat[555][555];
double lowc[555];
int pre[555];
bool vis[555];
double dist[555];
int n, s;
struct node
{
	int x, y;
}point[555];

void prim (int n, int s)
{
	memset (vis, 0, sizeof(vis));
	for (int i = 2; i <= n; ++i)
	{
		lowc[i] = mat[1][i];
		pre[i] = 1;
	}
	pre[1] = -1;
	vis[1] = 1;
	lowc[1] = 0;
	int cnt = 0;
	for (int i = 2; i <= n; ++i)
	{
		double minc = 0x3f3f3f3f;
		int p;
		for (int j = 1; j <= n; ++j)
		{
			if (!vis[j] && minc > lowc[j])
			{
				minc = lowc[j];
				p = j;
			}
		}
		dist[cnt++] = minc;
		vis[p] = 1;
		for (int j = 1; j <= n; ++j)
		{
			if (!vis[j] && mat[p][j] < lowc[j])
			{
				lowc[j] = mat[p][j];
				pre[j] = p;
			}
		}
	}
	sort (dist, dist + cnt);
	printf("%.2f\n", dist[cnt - s]);
}

double Dist (node a, node b)
{
	return sqrt (double ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)));
}

int main ()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &s, &n);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d", &point[i].x, &point[i].y);
		}
		memset (mat, 0x3f3f3f3f, sizeof(mat));
		for (int i = 1; i <= n; ++i)
		{
			for (int j = i + 1; j <= n; ++j)
			{
				mat[i][j] = mat[j][i] = Dist(point[i], point[j]);
			}
		}
		prim(n, s);
	}
	return 0;
}


POJ2349--Arctic Network

标签:

原文地址:http://blog.csdn.net/guard_mine/article/details/43114095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!