HDU1233 还是畅通工程【Kruskal算法+并查集】

还是畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 51751    Accepted Submission(s): 23482


Problem Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
 

Output
对每个测试用例,在1行里输出最小的公路总长度。
 

Sample Input
 
 
31 2 11 3 22 3 441 2 11 3 41 4 12 3 32 4 23 4 50
 
Sample Output
 
 
35
Hint
Hint
Huge input, scanf is recommended.
 

Source


问题链接:HDU1233 还是畅通工程

问题描述:参见上文

问题分析

  这是一个最小生成树的为问题,解决的算法有Kruskal(克鲁斯卡尔)算法和Prim(普里姆)算法

程序说明

  本程序使用Kruskal算法实现。有关最小生成树的问题,使用克鲁斯卡尔算法更具有优势,只需要对所有的边进行排序后处理一遍即可。程序中使用了并查集,用来判定加入一条边后会不会产生循环。程序中,图采用边列表的方式存储,按边的权从小到大顺序放在优先队列中,省去了排序。

  需要注意的是,优先队列申明的位置,以及它和结束条件(“count == n - 1”)的配合。对于每一个测试用例,开始是优先队列应该是空的。

  代码不够简洁,又写了一个简洁版。


AC的C++语言程序(简洁版)如下:

/* HDU1233 还是畅通工程 */

#include <iostream>
#include <queue>
#include <stdio.h>

using namespace std;

const int N = 100;
int f[N + 1];

void UFInit(int n)
{
    for(int i = 1; i <=n; i++)
        f[i] = i;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

bool Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
        return true;
    } else
        return false;
}

struct edge {
    int src, dest, cost;
    bool operator < (const edge& n) const {
        return cost > n.cost;
    }
};

int main()
{
    edge e;
    int n, m;

    while(scanf("%d", &n) != EOF && n) {
        priority_queue<edge> q;     // 优先队列,用于存储边列表

        UFInit(n);

        m = n * (n-1) / 2;

        // 构建优先队列
        while(m--) {
            scanf("%d%d%d", &e.src, &e.dest, &e.cost);
            q.push(e);
        }

        // Kruskal算法:获得最小生成树
        int ans=0, count=0;
        while(!q.empty()) {
            e = q.top();
            q.pop();

            if(Union(e.src, e.dest)) {
                count++;
                ans += e.cost;
            }

            if(count == n - 1)
                break;
        }

        // 输出结果
        printf("%d\n", ans);
    }

    return 0;
}



AC的C++语言程序如下:

/* HDU1233 还是畅通工程 */

#include <iostream>
#include <queue>
#include <cstdio>

using namespace std;

const int MAXN = 100;

// 并查集
int v[MAXN+1];
class UF {
    int length;
public:
    UF() {}

    // 压缩
    int Find(int x) {
        if(x == v[x])
            return x;
        else
            return v[x] = Find(v[x]);
    }

    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        if(x == y)
            return false;
        else {
            v[x] = y;
            return true;
        }
    }

    // 唯一树根判定连通性
    bool isconnect() {
        int root = -1;
        for( int i=1 ; i<=length ; i++ )
            if(root == -1)
               root = Find(i);
            else
                if(Find(i) != root)
                    return false;

        return true;
    }

    void reset(int n) {
        length = n;
        for(int i=0; i<=n; i++)
            v[i] = i;
    }
};

struct edge {
    int src, dest, cost;
    bool operator < (const edge& n) const {
        return cost > n.cost;
    }
};

int main()
{
    UF uf;
    edge e;
    int n, m;

    while(scanf("%d", &n) != EOF && n) {
        priority_queue<edge> q;     // 优先队列,用于存储边列表

        uf.reset(n);

        m = n * (n-1) / 2;

        // 构建优先队列
        while(m--) {
            scanf("%d%d%d", &e.src, &e.dest, &e.cost);
            q.push(e);
        }

        // Kruskal算法:获得最小生成树
        int ans=0, count=0;
        while(!q.empty()) {
            e = q.top();
            q.pop();

            if(uf.Union(e.src, e.dest)) {
                count++;
                ans += e.cost;
            }

            if(count == n - 1)
                break;
        }

        // 结果
        printf("%d\n", ans);
    }

    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值