1350:【例4-11】最短网络(agrinet)
发布时间
阅读量:
阅读量
例4-11
// 示例代码 Kruskal算法
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10005; // 定义常量 N,表示数组大小
struct point {
int x, y, v; // 记录边的两个顶点和权值
} a[N];
int f[105]; // 并查集数组,用于判断两个结点是否在同一连通块中
int n, m, x, k, ans; // n:图的结点数量,m:图中所有边的数量,x:当前读入的边的权值,k:已选边的数量,ans:当前生成树的总权值
int father(int x) { // 并查集查找祖先节点的函数
if (f[x] == x) return x; // 如果该结点的父亲节点就是它自己,直接返回
return f[x] = father(f[x]); // 否则,递归查找祖先节点
全部评论 (0)
还没有任何评论哟~
