标签:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<math.h>
#include<limits.h>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define max(a, b) a>b?a:b;
#define min(a, b) a<b?a:b;
#define INF 0xfffffff
const int N = 3000;
struct node
{
int x, y, l;
}maps[N];
int f[N], point, edge;
bool cmp(node a, node b)
{
return a.l<b.l;
}
int Find(int x)
{
if(f[x]!=x)
f[x]=Find(f[x]);
return f[x];
}
void Kruskal()
{
int i, ans;
ans=0;
for(i=0; i<edge; i++)
{
int ru=Find(maps[i].x);
int rv=Find(maps[i].y);
if(ru!=rv)
{
f[ru]=rv;
ans+=maps[i].l;
}
}
cout << ans << endl;
}
int main()
{
int i, j, a, b, c;
while(scanf("%d", &point), point)
{
scanf("%d", &edge);
for(j=0; j<=point; j++)
f[j]=j;
for(i=0; i<edge; i++)
{
scanf("%d%d%d", &a, &b, &c);
maps[i].x=a;
maps[i].y=b;
maps[i].l=c;
}
sort(maps, maps+edge, cmp);
Kruskal();
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/wazqWAZQ1/p/4681786.html