最小生成树 UVALive2515&UVA10369 Kruskal&Prim

最小生成树

一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边。

一、Kruskal

不断将两点不都在最小生成树中的当前最小权值边加入集合中,以形成最小生成树。

操作对象是边,适用于稀疏图。

上模板题:

UVALive2515

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P = 0 denotes the end of the input. The data sets are separated with an empty line. The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as ‘i j’ or as ‘j i’.

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3

1 2 37

2 1 17

1 2 68

3 7

1 2 19

2 3 11

3 1 7

1 3 5

2 3 89

3 1 91

1 2 32

5 7

1 2 5

2 3 7

2 4 8

4 5 11

3 5 10

1 5 6

4 2 12

0

Sample Output

0

17

16

26

模板:

结构体储存边的两端及权值,按权值由小到大排序,将两端点不都在最小生成树中的边计入。

结束hiahiahia

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxp=60;
const int maxc=100;
const int maxr=1e6;

int p,r;
int fa[maxp];

struct edge{
	int u,v,w;
}e[maxr];

bool cmp(edge e1,edge e2)
{
	return e1.w<e2.w;
}

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

int Kruskal()
{
	int ans=0,cnt=0;
	for(int i=0;i<=p;++i)
		fa[i]=i;

	sort(e,e+r,cmp);
	for(int i=0;i<r;++i){
		int u=e[i].u,v=e[i].v;
		int fu=find(u),fv=find(v);
		if(fu!=fv){
			ans+=e[i].w;
			fa[fu]=fv;
			if(++cnt>=p-1){
				return ans;
			}
		}
	}
}

int main()
{
	while(scanf("%d",&p)){
		if(!p) break;
		scanf("%d",&r);
		if(!r){
			printf("0\n");
			continue;
		}
		for(int i=0;i<r;++i){
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		}
		printf("%d\n",Kruskal());
	}

	return 0;
}

二、Prim

从任意一点出发,每次取最小生成树与未在集合内的点中距离(权值)最小的,将那一端点加入最小生成树。

操作对象是点,适用于稠密图。

板题:

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

从0出发遍历,用lowbit数组维护生成树节点到未生成树节点最小权值,每次取数组中未生成树节点中lowbit[i]最小的加入生成树中。

其余就根据题意来写了。

记录一个还没感受的东西:

O(m+nlogn)方法: 斐波那契堆或配对堆优化(__gnu_pbds::priority_queue)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxp=520;
const double inf=99999999999999.9;

int s,p;
int x[maxp],y[maxp];
double lowbit[maxp];	//当前最小生成树到i点的最小距离

double cal_dis(int i,int j)
{
	return sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j]-y[i])*(y[j]-y[i]));
}

double Prim()
{
	int now=0,nextn,cnt=0,j=0;
	int flag[maxp];
	double mi,ans[maxp];

	memset(flag,-1,sizeof(flag));
	flag[0]=0;

    while(++cnt<p){
		mi=inf;
		for(int i=1;i<p;++i){
			if(flag[i]){
				lowbit[i]=min(lowbit[i],cal_dis(now,i));
				if(lowbit[i]<mi){
					nextn=i;
					mi=lowbit[i];	//忘加这种语句真的蠢
				}
			}
		}
		flag[nextn]=0;
		ans[j++]=lowbit[nextn];
		now=nextn;
    }
    sort(ans,ans+j);

    return ans[p-s-1];
}

int main()
{
	int n;

	scanf("%d",&n);
	while(n--){
		for(int i=0;i<=maxp;++i){
			lowbit[i]=inf;
		}
		scanf("%d%d",&s,&p);
		for(int i=0;i<p;++i){
			scanf("%d%d",&x[i],&y[i]);
		}
		printf("%.2f\n",Prim());
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/DADDY_HONG/article/details/81460984
今日推荐