HDU1863-畅通工程

题目链接:点击打开链接

Problem Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。

Output

对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

Sample Input

 

3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100

Sample Output

 

3 ?

思路:刚开始没有看出来是最小生成树。。。看出来之后也没有用kruskal算法。

AC代码:

#include<iostream>
#include<queue>
#include<algorithm>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
using namespace std;
const int MAX = 500;
const int INF = 0X3f3f3f;

int n, m;
int father[MAX];//以下就是并查集模板

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

int findfather(int x) {
    int a = x;
    while(x != father[x]) {
        x = father[x];
    }
    while(a != father[a]) {
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}


struct node {
    int v;
    int u;
    int cost;
    node() {}
    node(int _v, int _u, int _cost) : v(_v), u(_u), cost(_cost) {}//构造函数
}stu[MAX];//存边

bool cmp(node a, node b) {//把所有边 按照从小到大排序
    return a.cost < b.cost;
}


int main() {
    int u, v, w;
    while(scanf("%d %d", &m, &n) != EOF) {
        if(m == 0)
            break;
        init();
        for(int i = 0; i < m; i++) {//输出所有的边
            scanf("%d %d %d", &stu[i].u, &stu[i].v, &stu[i].cost);
        }
        int ans = 0, num = 0;//边权之和,入树的边数
        sort(stu, stu + m, cmp);//排序
        for(int i = 0; i < m; i++) {//遍历边
            int faA = findfather(stu[i].u);
            int faB = findfather(stu[i].v);
            if(faA != faB) {//如果不在一个集合内就加入最小树
                father[faA] = faB;
                num++;//边数加1
                ans += stu[i].cost;//累加边权
            }
        }
        if(num == n -1)//树的特征,(判是否连通)
            cout << ans << endl;
        else
            cout << "?" << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ACMerszl/p/9572991.html