[POJ 2349] Arctic Network【Kruskal】

  • 2018-01-04
  • 0
  • 0

Problem:

Time Limit: 2000MS Memory Limit: 65536K

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

Solution:

这一题还是很水。。

本来想用 Prim 实现最小生成树,但是发现这道题用 Kruskal 处理更方便,在只剩 S 个连通块时可以直接中断输出答案即可。

Code: O(TP2logP2) [2140K, 172MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int T, S, P;
int x[502], y[502];

struct Edge{
	int u, v;
	double dis;
	
	inline bool operator < (const Edge &edge2) const {return dis < edge2.dis;}
} E[125002];

int tope, fa[502];

#define sqr(x) ((x) * (x))
inline double Euclid(int u, int v) {return sqrt((double)(sqr(x[u] - x[v]) + sqr(y[u] - y[v])));}

inline void ds_init() {for(register int i = 1; i <= P; i++) fa[i] = i;}

inline int ds_find(const int &u){
	if(fa[u] == u) return u;
	return fa[u] = ds_find(fa[u]);
}

inline bool ds_union(const int &u, const int &v){
	int ancu = ds_find(u), ancv = ds_find(v);
	if(ancu == ancv) return 0;
	fa[ancu] = ancv;
	return 1;
}

int main(){
	scanf("%d", &T);
	while(T--){
		scanf("%d%d", &S, &P);
		for(register int i = 1; i <= P; i++) scanf("%d%d", x + i, y + i);
		tope = 0;
		for(register int i = 1; i <= P; i++)
			for(register int j = i + 1; j <= P; j++)
				E[++tope].u = i, E[tope].v = j, E[tope].dis = Euclid(i, j);
		sort(E + 1, E + tope + 1);
		ds_init();
		int blockcnt = P;
		for(register int i = 1; i <= tope; i++)
			if(ds_union(E[i].u, E[i].v))
				if(--blockcnt == S){
					printf("%.2f\n", E[i].dis);
					break;
				}
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai