<九度 OJ>题目1028:继续畅通工程

题目描述:
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
输入:
    测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( 1< N < 100 );随后的 N(N-1)/2 行对应村庄间道路的成本及修建状态,每行给4个正整数,分别是两个村庄的编号(从1编号到N),此两村庄间道路的成本,以及修建状态:1表示已建,0表示未建。

    当N为0时输入结束。
输出:
    每个测试用例的输出占一行,输出全省畅通需要的最低成本。
样例输入:
3
1 2 1 0
1 3 2 0
2 3 4 0
3
1 2 1 0
1 3 2 0
2 3 4 1
3
1 2 1 0
1 3 2 1
2 3 4 1
0
样例输出:
3
1
0
分析:

还是并查集,运用并查集根据道路修建情况联立村庄,然后对边排序,由小到大检查每一条边的两个顶点的祖先,如果不一样,说明不是一个集合的,就将该边的费用累加...........(最小生成树----贪心的“kruskal算法”)


#include "vector"
#include "string"
#include "algorithm"
#include <iostream>
#include "stack"
#include <cmath>
#include <set>
 
using namespace std;
 
class Edge
{
public:
    int acity;//城市a
    int bcity;//城市b
    int cost;  //建成a到b的路的花费
    bool isBuild; //标记路是否建成
    bool operator < (const Edge &q) const//注意返回值的类型,运算符重载。
    {  
        return cost<q.cost;
    }
};
 
Edge edge[10000];
 
class UFSet
{
public:
    UFSet(int nsize)
    {
        size = nsize;
        parent = new int[size+1];
    };
    ~UFSet()
    {
        delete[] parent;
        parent = NULL;
    };
    void makeSet(int n);初始化每个元素的祖先  
    int findSet(int x);//找到元素x的祖先元素  
    void unionSet(int a, int b);//若两个元素的祖先不同,则将x元素的祖先设置为y元素的祖先  
    int getMinCost(int m);//获取最小花费 
private:
    int *parent;//存放祖先节点,例如x=parent[i],元素i的祖先节点为元素x  
    int size;
};
 
void UFSet::makeSet(int n) //初始化  
{
    //初始化每一个元素都各自为一个独立的集合,其祖先均设定为自身  
    for (size_t i = 1; i <= n; i++)
        parent[i] = i;
}
 
int UFSet::findSet(int x)
{
    //找到元素所在的集合,也就是找到自己的最高的祖先,  
    //这也是判断两个元素是否在同一个集合中的主要依据。  
    if (parent[x] == x)//递归截止条件(最高祖先的祖先是其自身)
        return x;
 
    parent[x] = findSet(parent[x]);//递归,最终找到x的最高祖先,并且沿途找到所有的最高祖先  
    return parent[x];
}
 
void UFSet::unionSet(int x, int y)
{
    //将x和y所在的集合进行合并,利用findSet()判断x和y所在的集合是否相同,  
    //如果不同,则要把其中一个元素的祖先指向另一个元素的祖先。  
    int ux = findSet(x);//获取节点x的祖先  
    int uy = findSet(y);
    if (ux != uy)
        parent[ux] = uy;
}
int UFSet::getMinCost(int m)
{
    sort(edge, edge + m);//必须先对边排序,这样才能贪心的形成最小花费
    int sum = 0;
    for (int i = 0; i<m; i++)
    {
        int baseA = findSet(edge[i].acity);//找到城市a的祖先
        int baseB = findSet(edge[i].bcity);
        if (baseA != baseB)
        {
            parent[baseA] = baseB;//将城市a的祖先设置成b的祖先
            sum += edge[i].cost;
        }
    }
    return sum;
}
 
int main()
{
    int n = 0;
    while (cin >> n, n > 0)
    {
        int m = n*(n - 1) / 2;
 
        UFSet uset(10000);
        uset.makeSet(n);//初始化
        for (int i = 0; i < m; i++)
        {
            cin>> edge[i].acity>> edge[i].bcity>> edge[i].cost>> edge[i].isBuild;
            if (edge[i].isBuild == 1)//将已经建成的两个城市建立连接
                uset.unionSet(edge[i].acity, edge[i].bcity);
        }
        int mincost = uset.getMinCost(m);
        cout << mincost << endl;
    }
 
    return 0;
}
/**************************************************************
    Problem: 1028
    User: EbowTang
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:1676 kb
****************************************************************/


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50517739

原作者博客:http://blog.csdn.net/ebowtang


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值