POJ1287 Networking (Kruskal与并查集求解最小生成树)

该博客详细介绍了如何运用图论中的Kruskal算法和并查集数据结构来解决POJ1287这个网络连接问题。通过实例解析输入、输出格式,并提供了完整的解决方案。


Description

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
题目大意:输入点的个数和和边的个数,并输入每条边连接的两个点和该边的权值。输出最小生成树的代价
由于此题输入的就是边的信息,所以考虑用Kruskal算法实现,Kruskal的核心是,将边按权值大小进行排序,然后开始for,如果一个边的两点已经连通,就跳到下一条边,如果没有连通就把这两个点用这条边连起来,一直加到n-1条边。可以证明,求出来的就是最小生成树。
至于如何判断两点是否已经连通,则要用到并查集,这里只用到了最简单的并查集。并查集的最根本也是一棵树,输入两个点后,查找这两个点的根节点,看是否是一个节点。但这样的话时间消耗将会非常巨大,所以要压缩路径。由于在这个问题中,我们只关心一个节点的根节点是多少,所以中间的父亲节点可以用一个递归一次性全部提上去,再查找过两次过后,这个算法的时间复杂度就几乎就是O(1)了。合并两个集合就更简单了,只需要一个节点的父亲等于另一个节点即可。
#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

int fa[10005];

struct line0//结构体存边
{
    int left;
    int right;
    int len;
}line[10005];

bool cmp(line0 x0,line0 y0)//结构体排序
{
    if (x0.len<=y0.len)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int findfa(int x)//并查集的查找兼压缩路径
{
   return fa[x] == x ? x : (fa[x] = findfa(fa[x]));
}


int main()
{
    int p,r,ans,cnt,i,nows,fl,fr;
    while(1)
    {
        scanf("%d",&p);
        if (p==0)
        {
            break;
        }
        for (i=1;i<=p;i++)
        {
            fa[i]=i;
        }
        scanf("%d",&r);
        ans=0;
        cnt=0;
        nows=0;
        for (i=0;i<=r-1;i++)
        {
            scanf("%d%d%d",&line[i].left,&line[i].right,&line[i].len);
        }
        sort(line,line+r,cmp);
        while(1)
        {
            if (cnt==p-1)
            {
                break;
            }
            fl=findfa(line[nows].left);
            fr=findfa(line[nows].right);
            if (fl!=fr)
            {
                fa[findfa(fl)]=findfa(fr);//合并
                cnt++;
                ans=ans+line[nows].len;
            }
            nows++;
        }
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值