/*
Name:连接岛屿问题 (MST Kruskal算法)
Actor:HT
Time:2015年8月13日
Error Reporte:
*/
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#define N 50
using namespace std;
//最小生成树:
//依次选择可用的最小边连接,直到结束
//反证法:
//如果存在不用最小边的最佳结果,
//那么连入最小边以后,原结果成环;
//去掉可去的任意一个边,都比去掉最小边要长
//所以可证,要取最小边
struct edge //以边为研究对象
{
int x, y, l;
}ed[N];
int pa[N];
int s;
bool cmp(edge a, edge b)
{
return (a.l < b.l);
}
int findpa(int a) //并查集.递归找根
{
if (pa[a] == a) return a;
else return findpa(pa[a]);
}
int main()
{
int i, j;
int n;
int a, b, len;
int sum,tempa,tempb;
while (scanf("%d %d", &s, &n) != EOF)
{
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d %d %d", &a, &b, &len);
ed[i].x = a;
ed[i].y
[图论]连接岛屿问题(MST.Kruskal算法 并查集)
最新推荐文章于 2023-07-25 15:07:39 发布