感谢博主:https://www.cnblogs.com/aiguona/p/7223625.html?utm_source=itdadao&utm_medium=referral写的挺好的,建议大家看看。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m,sum;
struct node
{
int start,end,power;//start为起始点,end为终止点,power为权值
} edge[5050];
int pre[5050];
int cmp(node a, node b)
{
return a.power<b.power;//按照权值排序
}
int find(int x)//并查集找祖先
{
if(x!=pre[x])
{
pre[x]=find(pre[x]);
}
return pre[x];
}
void merge(int x,int y,int n)//并查集合并函数,n是用来记录最短路中应该加入哪个点
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
pre[fx]=fy;
sum+=edge[n].power;
}
}
int main()
{
while(~scanf("%d", &n),