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;
}


 

 

 

發佈了104 篇原創文章 · 獲贊 8 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章