克鲁斯卡尔
struct edge { int u, v, w; }e[maxn]; int f[110]; bool cmp(edge a, edge b) { return a.w < b.w; } int find(int x) { if(x != f[x]) return f[x] = find(f[x]); return f[x]; } int MST() { int sum = 0; for(int i = 0; i < m; i++) { int x = find(e[i].u); int y = find(e[i].v); if(x != y) { sum += e[i].w; f[x] = y; } } return sum; }
原文地址:http://blog.csdn.net/u011686226/article/details/26500193