poj 2349 Arctic Network

这道题的题意貌似有点小难啊~

题目大意:一个组织要实现几个outposts(后称岗哨)之间的通信,每两个岗哨之间有transceiver(后称接收器)且距离少于D便可以实现通信。

我们要分清接收器(S)和岗哨(P),我的目的就是求出那个可以实现所有岗哨之间相互通信的最小的D。

其实岗哨数就是连通分支的个数,有多少个岗哨,就可以有多少个连通分支。

这样每去 掉一条边,我们就多了一个分支。那么我们就可以去掉s-1条最长边,得到s个连通分支。 

(每个分支里有一个岗哨)剩下的最长边则为所求了。 

所以,用PRIM和KRUSKAL都可以解决

 

                                                                                                                                           Arctic Network
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7059   Accepted: 2393

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

 

 

#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int M = 25003;

int n,m;
int father[M];
int num[M];

struct node{
	int s,e,v;
	//bool visit;	
}edge[M];

bool cmp( node a , node b )
{
	return a.v < b.v;	
}

int find( int x )
{
	return x == father[x] ? x : father[x] = find( father[x] );	
}

bool merge( int a , int b )
{
	int x,y;
	x = find( a );
	y = find( b );
	if( x == y )
		return true;
	else
		father[x] = y;
	return false;
}

int main()
{
	while( scanf("%d%d",&n,&m) == 2 ){
		for( int i=1 ; i<=n ; i++ )
			father[i] = i;
		for( int i=1 ; i<=m ; i++ ){
			scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].v);	
		}
		sort( edge+1 , edge+1+m , cmp );
		int minn,l;
		l = minn = 0;
		for( int i=1 ; i<=m ; i++ ){
			if( merge(edge[i].s , edge[i].e ) )
				continue;
			else{
				minn = edge[i].v;
				num[l++] = i;
			}
		}
		printf("%d\n%d\n",minn,l);
		for( int i=0 ; i<l ; i++ )
			printf("%d %d\n",edge[num[i]].s,edge[num[i]].e);
	}
	return 0;
}


 

 

 

你可能感兴趣的:(poj 2349 Arctic Network)