DHU 1301 Jungle Roads Minimum Spanning Tree

Title:

This topic is very, very long. . . . .
It's just that there are some villages, the roads between them need to be repaired, they want to spend the least amount of money to repair, and to keep all the villages roads, there will be no roads if they are not repaired.

analyze

For the minimum spanning tree problem, I only have one Kruskal, Kruskal algorithm.
It's a board question, the comments have explanations

Code:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
#define maxn 100
#define ll long long
#define debug cout<<"***********"<<endl;
using namespace std;
struct Edge{ //结点结构体
    int u,v,w;
    Edge(){}
    Edge(int u,int v,int w){//构造方法
        this->u = u;
        this->v = v;
        this->w = w;
    }
    bool operator < (const Edge &a){//排序规则
        return w < a.w;
    }
};
char a,b;
Edge e[maxn];
int pa[90];

void init(){//并查集初始化
    for(int i = 0; i <= 30; i++){
        pa[i] = i;
    }
}

int findset(int x){//寻找集合代表并进行路径压缩
    return x == pa[x] ? x : pa[x] = findset(pa[x]);
}

int main(){
    std::ios::sync_with_stdio(false);
    int n,m,x,w;
    while(cin>>n && n){
        m = 0;
        int cnt = n;
        n--;
        while(n--){
            cin>>a>>x;
            int u = a - 'A' + 1;
            while(x--){
                cin>>b>>w;
                int v = b - 'A' + 1;
                e[m++] = Edge(u,v,w);//添加边,并记录边的个数。
            }
        }

        sort(e,e+m);
        int cost = 0;//花费
        init();

        for(int i = 0; i < m; i++){//贪心,每次找最小花费的边,如果是不同树中的,添加到最小生成树中
            Edge u = e[i];
            int x = u.u;
            int y = u.v;
            x = findset(x);
            y = findset(y);
            if(x != y){
                cost += u.w;
                pa[x] = y;
                cnt--;
            }
            if(cnt == 1){
                break;
            }
        }
        cout<<cost<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325396530&siteId=291194637