数据结构:kruskal算法

#include 
#include 
#include 
using namespace std;

typedef struct {
    int begin;
    int end;
    int weight;
} Edge;
int father[1005];

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

int find(int x) {
    return father[x] == x ? x : father[x] = find(father[x]);
}

void join (int x, int y) {
    int p1 = find(x);
    int p2 = find(y);

    if (p1 == p2) {
        return ;
    }
    father[p1] = p2;
}

void kruskal (Edge edges[], int n, int m) {
    int sum_weight = 0;
    int num = 0;
    init();
    for (int i=0; i= n-1) { break; }
    }
    printf("weight of MST is %d\n", sum_weight);

}

bool cmp (Edge a, Edge b) {
    return a.weight < b.weight;
}

int main (int argc, char **argv) {
    int n, m;//顶点、边
    Edge edges[20];
    scanf("%d %d", &n, &m);
    for (int i=0; i

你可能感兴趣的:(数据结构)