天天看點

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