最小生成树总结+例题

最小生成树是连接一个图中所有点的最短距离。

克鲁斯卡尔算法:找到权值最小的边相连,但要确保连上这条边后不会构成回路。
这里的判断回路用到并查集。
Prim算法:随便选一个点作为起点,找到与这个点相连的权值最小的边将其连接,用一个数组标记哪些顶点已经加入了生成树。继续找离这两个点权值最小的边将其连接,重复n-1次操作。
这里的算法与Dijkstra算法相似,这里计算的最短距离是“生成树”到各个顶点的距离。

例题

1.畅通工程再续 HDU - 1875
链接
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
Sample Input
2
2
10 10
20 20
3
1 1
2 2
1000 1000
Sample Output
1414.2
oh!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
int f[105];
int n;
struct node{
	int x,y;
	double z;
};
int a[105][105];
double diss(node a,node b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int getf(int x){
	if(x==f[x])return x;
	return f[x]=getf(f[x]);
}
double cmp(node a,node b){
	return a.z<b.z;
}
int main(){
	int c;
	cin>>c;
	while(c--){
		scanf("%d",&n);
		node e[10005],d[10005];
		int m=1;
		for(int i=1;i<=n;i++){
			scanf("%d%d",&e[i].x,&e[i].y);
			for(int j=1;j<i;j++){
				double c=diss(e[i],e[j]);
				if(c>=10&&c<=1000){
					d[m].x=i;
					d[m].y=j;
					d[m].z=c;
					m++;
					d[m].x=i;
					d[m].y=j;
					d[m].z=c;
					m++;
				}
			}
		}
		sort(d+1,d+m,cmp);
		memset(f,0,sizeof(f));
		for(int i=1;i<=n;i++){
			f[i]=i;
		}
		double ans=0;
		for(int i=1;i<m;i++){
			int x=getf(d[i].x);
			int y=getf(d[i].y);
			if(x==y)continue;
			f[x]=y;
			ans+=d[i].z;
		//	cout<<ans<<"aa"<<endl;
		}
		ans*=100;
		if(ans!=0)printf("%.1lf\n",ans);
		else printf("oh!\n");
	}
	return 0;
}

2. Highways POJ - 1751
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can’t reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.

Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.

The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.
Output
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2
Sample Output
1 6
3 7
4 9
5 7
8 3
题意:已知有n个城镇及它们的坐标,且已有m个条路已经修通,输出若使所有的城镇连接起来并且使所修公路的距离最短需要修建哪些公路。

prim算法适用于稠密图,对于已联通的两点,距离为零。建图时不需要开根号。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f;
struct node{
	int x,y;
}a[755];
int n,m;
int diss(node a,node b){
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int vis[755],mp[755][755],dis[755],e[755];
void prim(){
	for(int i=1;i<n;i++){
		int minn=inf;
		int point_minn;
		for(int j=1;j<=n;j++){
			if(vis[j]==0&&minn>dis[j]){
				minn=dis[j];
				point_minn=j;//找到离“生成树”最近的点 
			}
		}
		vis[point_minn]=1;
		for(int k=1;k<=n;k++){
			if(vis[k]==0&&dis[k]>mp[point_minn][k]){
				e[k]=point_minn;//在k与point_minn建立一条边 
				dis[k]=mp[point_minn][k];
			}
		}
		if(mp[e[point_minn]][point_minn]){
			printf("%d %d\n",e[point_minn],point_minn);
		}
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
		for(int j=1;j<i;j++){
			mp[i][j]=mp[j][i]=diss(a[i],a[j]);
		}
		mp[i][i]=inf;
	}
	scanf("%d",&m);
	int xx,yy;
	for(int i=1;i<=m;i++){
		scanf("%d%d",&xx,&yy);
		mp[xx][yy]=mp[yy][xx]=0;
	} 
	memset(vis,0,sizeof(vis));
	vis[1]=1;
	for(int i=1;i<=n;i++){
		dis[i]=mp[1][i];
		e[i]=1;
	}
	prim();
}

3.P1265 公路修建
某国有n个城市,它们互相之间没有公路相通,因此交通十分不便。为解决这一“行路难”的问题,政府决定修建公路。修建公路的任务由各城市共同完成。

修建工程分若干轮完成。在每一轮中,每个城市选择一个与它最近的城市,申请修建通往该城市的公路。政府负责审批这些申请以决定是否同意修建。

政府审批的规则如下:

(1)如果两个或以上城市申请修建同一条公路,则让它们共同修建;

(2)如果三个或以上的城市申请修建的公路成环。如下图,A申请修建公路AB,B申请修建公路BC,C申请修建公路CA。则政府将否决其中最短的一条公路的修建申请;

(3)其他情况的申请一律同意。

一轮修建结束后,可能会有若干城市可以通过公路直接或间接相连。这些可以互相:连通的城市即组成“城市联盟”。在下一轮修建中,每个“城市联盟”将被看作一个城市,发挥一个城市的作用。

当所有城市被组合成一个“城市联盟”时,修建工程也就完成了。

你的任务是根据城市的分布和前面讲到的规则,计算出将要修建的公路总长度。

思路:裸的prim算法,与上题不同的是,这个不能用5000*5000的矩阵将权值存起来,否则会mle,只需要找到这个点时再计算权值就好了。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=5001;
int n,x[maxn],y[maxn];
double cnt,d[maxn];
bool b[maxn];
double dis(int i,int j)
{
    return sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j]));
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",x+i,y+i);
    for(int i=1;i<=n;i++)
        d[i]=dis(1,i);
    int k;
    for(int i=1;i<=n-1;i++)
    {
        double mn=1e9;
        for(int j=1;j<=n;j++)
            if(d[j]&&d[j]<mn)
            {
                mn=d[j];
                k=j;
            }
        d[k]=0;
        cnt+=mn;
        for(int j=1;j<=n;j++)
            if(dis(k,j)<d[j])
                d[j]=dis(k,j);
    }
    printf("%.2f\n",cnt);
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
prim算法是一种用于解决最小生成树(Minimum Spanning Tree,MST)问题的算法。最小生成树是指在一个连通加权图中找到一棵包含所有顶点并且边权值之和最小的树。 下面以一个例题图来解释prim算法的过程。假设我们有一个加权图,顶点分别为A、B、C、D、E,边的权值为: AB: 2 AC: 3 AD: 7 BC: 8 BE: 4 CE: 5 DE: 6 首先选择一个任意顶点作为起始点,我们选择A点作为起始点。将A点标记为已访问,然后找到与A点相邻的边中权值最小的边,即AB,将B点标记为已访问。此时A—B这条边就成为了最小生成树的一部分。 接下来,我们需要找到与A、B点相邻的边中权值最小的边。分别是AC和BE,我们选择AC这条边,将C点标记为已访问。此时A—B和A—C这两条边就成为了最小生成树的一部分。 然后,我们找到与A、B、C点相邻的边中权值最小的边。分别是AD和CE,我们选择CE这条边,将E点标记为已访问。此时A—B、A—C和C—E这三条边就成为了最小生成树的一部分。 最后,我们找到与A、B、C、E点相邻的边中权值最小的边,即DE。将D点标记为已访问。此时A—B、A—C、C—E和D—E这四条边就组成了最小生成树。 通过上述过程,我们得到了最小生成树,其包含了ABCED这5个顶点,使得边的权值之和最小。这就是prim算法的过程,通过不断选择最小的边来构建最小生成树

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值