[POJ 2349] Arctic Network【Kruskal】

  • 2018-01-04
  • 0
  • 0


Time Limit: 2000MS Memory Limit: 65536K


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.


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).


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

2 4
0 100
0 300
0 600
150 750

Sample Output



Waterloo local 2002.09.28



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

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

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);
		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);
		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);
	return 0;



新博客地址: darkleafin.cf



OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.


- Theme by Qzhai