poj1861最小生成树(并查集)-kruskal算法

Kruskal算法是一种按网中边的权值递增的顺序构造最小生成树的方法。其基本思想是:设无向连通网为G(V,E),令G的最小生成树为T,其初态为T=(V,{}),即开始时,最小生成树T

由图G中的n个点构成,顶点之间没有一边,这样T个边顶点各自构成一个连通分量。然后,按照边的权值由小到大的顺序,考察G的边值E中个条边。若被考察的边的两个顶点属于T的两个不同连通分量,则将此边作为最小生成树的边加入T中,同时把两个连通分量连成一个连通分量,若被考察的边的两个顶点属于同一个分量,则舍去次边,以免造成回路,如此下去,当T中的连通分量个数为1时,此时连通分量便为G的一棵最小生成树。

Kruskal算法的实现:

设置一个结构体数组edge存储网中所有的边,边的结构类型包括构成的顶点信息和边的权值,定义如下:

#define maxsize 1000;

typedef struct{

datatype v1,v2;

int cost;

}edge[maxsize];

并查集

int find(int x)

{

  return x==father[x]?find(father[x]):x;

}

下面以poj1861 Network题为列,对Kruskal算法的了解。

题目:

Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). 
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections. 
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied. 

input:

The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.

output

Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.

这个题就是典型的Kruskal算法的应用。

本题代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
       int v1,v2,c;
       }edge[15001],a[1001]; //edge[i],代表网中的一边。
          
int n,m,f[1001];

int find(int a)               //并查值。 
{
   return f[a]==a?a:find(f[a]); 
}

bool cmp(node a,node b)
{
     if(a.c!=b.c) return a.c<b.c;
     if(a.v1!=b.v1) return a.v1<b.v1;
     return a.v2<b.v2;
}

void kruskal()
{
     int i,k=0;
     for(i=1;i<=n;i++)
        f[i]=i;     
     for(i=0;i<m;i++)
     {
       int x=find(edge[i].v1);
       int y=find(edge[i].v2);
       if(x!=y)
       {
        f[x]=y;
        a[++k]=edge[i];
        }
     }
      if(k==n-1) return;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
      for(int i=0;i<m;i++)
       scanf("%d%d%d",&edge[i].v1,&edge[i].v2,&edge[i].c);
       sort(edge,edge+m,cmp);
       kruskal();
        printf("%d\n%d\n",a[n-1].c,n-1);
       for(int i=1;i<n;i++)
       printf("%d %d\n",a[i].v1,a[i].v2);
      }
      return 0;
}
      




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值