Kruskal 算法 求最小生成树

来源:互联网 发布:公司企业网络架构部署 编辑:程序博客网 时间:2024/04/28 06:57

Kruskal算法基于贪心的思想,对于图G={V,E}先构造G‘={V,非空},然后依次向G’中添加E未添加过的权值的最小的边,如果这条边加入G‘中存在环,则去掉这条边,直到G’成为一棵树。

具体步骤:

(1)首先初始化,生成图G'并将E中的边按权值排序;

(2)从最小的边开始尝试加入到图G‘中,如果当前边加入后存在环,则放弃当前边,否则标记当前边并计数。

(3)遍历所有的边后,如果选择的边数等于n-1,则生成最小生成树,计算步骤(2)所选择的边的权值之和。

下面使用前向星和并查集实现Kruskal算法:

Description

The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some 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. Each highway connects exactly two towns. All highways follow straight lines. 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 length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

Input

The first line of input is an integer T, which tells how many test cases followed. 
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

Output

For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

Sample Input

130 990 692990 0 179692 179 0

Sample Output

692
本题的题目意思是要在一些城市之间修高速公路,要求所有的城市必须连通。题目关心的是最长的那条边的是多长,其他要求没有。要想保证连通,至少要形成一棵树,也就是说这课树的最长边最小,最终输出这条最长边的长度。
#include <iostream>#include <algorithm>#include <stdio.h>using namespace std;const int Max=502;struct  data{    int u,v,w;}edge[Max*Max];int n,m,pa[Max];bool cmp(data a,data b){    if(a.w<b.w)       return true;    return false;}void make_set(){    for(int x=0;x<n;x++)       pa[x]=x;}int find_set(int x){    if(x!=pa[x])        pa[x]=find_set(pa[x]);    return pa[x];}int kruskal(){    int i,ans=0;    make_set();    sort(edge,edge+m,cmp);    for(i=0;i<m;i++)    {        int x=find_set(edge[i].v);        int y=find_set(edge[i].u);        if(x!=y)        {           pa[y]=x;           ans=edge[i].w;        }    }    return ans;}int main(){    int t,i,j,val;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        m=0;        for(i=0;i<n;i++)           for(j=0;j<n;j++)           {               scanf("%d",&val);               if(j>i)               {                   edge[m].u=i;                   edge[m].v=j;                   edge[m].w=val;                   m++;               }           }           printf("%d\n",kruskal());    }    return 0;}


0 0
原创粉丝点击