Arctic Network题解+(最小生成树二次理解 )

由于在上一篇文章用了大量的文字已经对于最小生成树的两种算法(克鲁斯卡尔和普利姆算法)做了基础的讲解,下面的话我就大概说一下思想和解题步骤,然后再在附加上一个昨天做题(虽然说很基础但是对于初学的我就有点……)的一个题解;
最小生成树第一次讲述的连接:https://blog.csdn.net/weixin_44606952/article/details/99301454
克鲁斯卡尔算法:
就是把各条路径的距离先进行简单的排序(从小到大),我们的目的是找到最短长度把所有的城市都连通起来,说明白点就是求所有路径之间的总和,有n个顶点我们就需要n-1条线路把这几个点连接起来,连接的思想很简单,遍历每一条边,要是没有在建成的树上的话,就把这一条边加上(因为已经排过序所以它一定是当前的最短路)当找到n-1条边的时候就结束,代表已经找够了;用到的算法有:快排,并查集上一篇文章里面有相关的连接不懂的可以去看看:
普里姆算法 :
先把所有点到1号点的距离存入dis数组,在后续的过程中依然使用最短路径的思想将dis数组中的数据做出改变,不过数组中的值代表的含义有发生改变他表示的是当前节点和连着他的上一个节点的距离遍历完所有节点就结束
Arctic Network讲解:
原文:
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

题意:
有一些前线站点是通过无线通信(传达信号与收发器的功率有关)来互相传信息的,有些是通过卫星的卫星可以不受距离的限制,但是无线就不行了,现在给出建立卫星的前哨站数目,和所有的前哨站数问需要的收发器能满足使用的当前收发器最小功率的距离应该是多少
解题思路:
先用普利姆算出各个点之间的距离存入dis数组,对于数组进行排序,排好之后用m-1条边减去用无线电信号连接的那一条边剩下的最大边就是要找的距离D了也就是输出dis[m-n+1]
ACa代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
double e[2000][2000];
double dis[20000];
int book[20000];
int inf=99999999;
struct node{
	double x;
	double y;
}s[2000];
int main()
{
	int n,m,t;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d %d", &n,&m);
	    double min, sum=0;
	    for(int i=1; i<=m; i++)
	    scanf("%lf %lf", &s[i].x,&s[i].y);
		for(int i=1; i<=m; i++)
	      for(int j=1; j<=m; j++)
	  	    e[i][j]=e[j][i]=sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)+
			  (s[i].y-s[j].y)*(s[i].y-s[j].y));
	 for(int i=1; i<=m; i++)
	    dis[i]=e[1][i];
	memset(book,0,sizeof(book));
	int count=0,j;
	count++; book[1]=1;
	while(count<m)
	{
		min=inf;
		for(int i=1; i<=m; i++)
		{
			if(book[i]==0&&dis[i]<min)
			{
				min=dis[i]; j=i;
			}
		}
		book[j]=1;count++;
		for(int k=1; k<=m; k++)
		{
			if(book[k]==0&&dis[k]>e[j][k])
			dis[k]=e[j][k];
		}
    }
    double p;//排序
	for(int i=1; i<m; i++)
	{
		for(int j=i+1; j<=m; j++)
    	{
    		if(dis[i]>=dis[j])
    		{
    			p=dis[i]; dis[i]=dis[j];dis[j]=p;
			}
		}
	}
	 printf("%.2f\n", dis[m-n+1]);
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮皮皮皮皮皮卡乒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值