poj 2349 K - Arctic Network 【最小生成树prime】

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

题意:有s个卫星和p个村庄,将p个村庄连通,最少需要p-1条边,现在s个卫星可以替代s-1条边,剩下的边的权值,只要在d权值内都可以连通,求剩下p-s条边连通的最小代价d是多少?

思路:最小生成树,求第p-s条最小边。计算出所有村庄线路的权值以后,建最小生成树,将每一次的权值存入数组,最后升序排序,输出第p-s个数。

总结:稠密图用prime算法更加高效。稀疏图用kruskal

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<queue>
#include<string>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 590;
int book[maxn];
double map1[maxn][maxn],dis[maxn],head[maxn];
int n,m,ans,cnt;
struct node{
	int from,to;
	double w;
}edge[maxn*3];

struct Node{
	int x,y;
}num[maxn];

double Count(struct Node a,struct Node b)
{
	return  sqrt((a.x -b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y)*1.0);
}

void Init()
{
	int i,j;
	double w;
	for(i = 1; i <= m; i ++)
	{
		for(j = 1; j < i; j ++)
		{
			w = Count(num[i],num[j]);
			map1[i][j] = map1[j][i] = w;
		}
	}
	return;
}
double Prime()
{
	int i,j,u,first,count;
	double minn;
	memset(book,0,sizeof(book));
	for(i = 1; i <= m; i ++)
		dis[i] = map1[1][i];
	book[1] = 1;
	count = 0;
	for(i = 1; i < m; i ++)
	{
		minn = inf;
		for(j = 1; j <= m; j ++)
		{
			if(dis[j] < minn&&!book[j])
			{
				u = j;
				minn = dis[j];
			}
		}
		book[u] = 1;
		head[++count] = minn;
		for(j = 1; j <= m; j ++)
		{
			if(dis[j] > map1[u][j])
			{
				dis[j] = map1[u][j];
			}
		}
	}
	sort(head+1,head+count+1);
	return head[m-n];
}
int main()
{
	int t;
	int x,y,i,j,count;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i = 1; i <= m; i ++)
			for(j = 1; j <= m; j ++)
				if(i == j)
					map1[i][j] = 0;
				else
					map1[i][j] = inf;
					
		for(i = 1; i <= m; i ++)
			scanf("%d%d",&num[i].x,&num[i].y);
			
		Init();
		printf("%.2lf\n",Prime());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hello_sheep/article/details/80213128
今日推荐