HDU1863 畅通工程【Kruskal算法+并查集】

 

畅通工程

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33818    Accepted Submission(s): 14964


 
Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 
 
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 
 
Sample Input
 
3 31 2 11 3 22 3 41 32 3 20 100
 
Sample Output
 
3?
 
 
Source

 

 

 

问题链接:HDU1863 畅通工程

问题描述:参见上文。

问题分析

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

程序说明

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

 

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

 

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

/* HDU1863 畅通工程 */

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

using namespace std;

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

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

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;
        cnt--;
        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%d", &n, &m) != EOF && n) {
        priority_queue<edge> q;     // 优先队列,用于存储边列表

        UFInit(m);

        // 构建优先队列
        while(n--) {
            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 == m - 1)
                break;
        }

        // 连通性判定,输出结果
        if(cnt == 1)
            printf("%d\n", ans);
        else
            printf("?\n");
    }

    return 0;
}

 

 

 

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

 

/* HDU1863 畅通工程 */

#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%d", &n, &m) != EOF && n) {
        priority_queue<edge> q;     // 优先队列,用于存储边列表

        uf.reset(m);

        // 构建优先队列
        while(n--) {
            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 == m - 1)
                break;
        }

        // 连通性判定,输出结果
        if(uf.isconnect())
            printf("%d\n", ans);
        else
            printf("?\n");
    }

    return 0;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值