帮我给以下代码写注释void swap(int* a, int* b) { int tmp = *a; *a = *b, *b = tmp; } struct DisjointSetUnion { int *f, *size; int n, setCount; }; void initDSU(struct DisjointSetUnion* obj, int n) { obj->f = malloc(sizeof(int) * n); obj->size = malloc(sizeof(int) * n); obj->n = n; obj->setCount = n; for (int i = 0; i < n; i++) { obj->f[i] = i; obj->size[i] = 1; } } int find(struct DisjointSetUnion* obj, int x) { return obj->f[x] == x ? x : (obj->f[x] = find(obj, obj->f[x])); } int unionSet(struct DisjointSetUnion* obj, int x, int y) { int fx = find(obj, x), fy = find(obj, y); if (fx == fy) { return false; } if (obj->size[fx] < obj->size[fy]) { swap(&fx, &fy); } obj->size[fx] += obj->size[fy]; obj->f[fy] = fx; obj->setCount--; return true; } int connected(struct DisjointSetUnion* obj, int x, int y) { return find(obj, x) == find(obj, y); } struct Tuple { int x, y, z }; int cmp(const struct Tuple* a, const struct Tuple* b) { return a->z - b->z; } int minimumEffortPath(int** heights, int heightsSize, int* heightsColSize) { int m = heightsSize; int n = heightsColSize[0]; struct Tuple edges[n * m * 2]; int edgesSize = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { int id = i * n + j; if (i > 0) { edges[edgesSize].x = id - n; edges[edgesSize].y = id; edges[edgesSize++].z = fabs(heights[i][j] - heights[i - 1][j]); } if (j > 0) { edges[edgesSize].x = id - 1; edges[edgesSize].y = id; edges[edgesSize++].z = fabs(heights[i][j] - heights[i][j - 1]); } } } qsort(edges, edgesSize, sizeof(struct Tuple), cmp); struct DisjointSetUnion* uf = malloc(sizeof(struct DisjointSetUnion)); initDSU(uf, m * n); int ans = 0; for (int i = 0; i < edgesSize; i++) { unionSet(uf, edges[i].x, edges[i].y); if (connected(uf, 0, m * n - 1)) { ans = edges[i].z; break; } } return ans; }
时间: 2023-06-17 16:04:47 浏览: 101
c语言swap(a,b)值交换的4种实现方法
5星 · 资源好评率100%
/*
这个函数实现了两个数字交换的功能
输入:int* a, int* b, 分别是需要交换的两个数字的指针
输出:无
*/
void swap(int* a, int* b) {
int tmp = *a;
*a = *b, *b = tmp;
}
/*
这个结构体实现了并查集的功能
包括初始化,查找根节点,合并两个集合,判断两个元素是否属于同一个集合
*/
struct DisjointSetUnion {
int *f, *size; // f数组表示每个元素的父节点,size数组表示每个集合的大小
int n, setCount; // n表示元素个数,setCount表示集合个数
};
/*
初始化并查集
输入:DisjointSetUnion* obj, 指向需要初始化的并查集的指针;int n,元素个数
输出:无
*/
void initDSU(struct DisjointSetUnion* obj, int n) {
obj->f = malloc(sizeof(int) * n);
obj->size = malloc(sizeof(int) * n);
obj->n = n;
obj->setCount = n;
for (int i = 0; i < n; i++) {
obj->f[i] = i;
obj->size[i] = 1;
}
}
/*
查找元素所在集合的根节点
输入:DisjointSetUnion* obj, 指向并查集的指针;int x,需要查找的元素
输出:x所在集合的根节点
*/
int find(struct DisjointSetUnion* obj, int x) {
return obj->f[x] == x ? x : (obj->f[x] = find(obj, obj->f[x]));
}
/*
合并两个集合
输入:DisjointSetUnion* obj, 指向并查集的指针;int x, int y, 需要合并的两个元素
输出:合并是否成功,成功返回1,失败返回0
*/
int unionSet(struct DisjointSetUnion* obj, int x, int y) {
int fx = find(obj, x), fy = find(obj, y);
if (fx == fy) {
return false;
}
if (obj->size[fx] < obj->size[fy]) {
swap(&fx, &fy);
}
obj->size[fx] += obj->size[fy];
obj->f[fy] = fx;
obj->setCount--;
return true;
}
/*
判断两个元素是否属于同一个集合
输入:DisjointSetUnion* obj, 指向并查集的指针;int x, int y,需要判断的两个元素
输出:是否属于同一个集合,是返回1,否返回0
*/
int connected(struct DisjointSetUnion* obj, int x, int y) {
return find(obj, x) == find(obj, y);
}
/*
这个结构体表示边
包括边的起点,终点和权值
*/
struct Tuple {
int x, y, z };
/*
比较边的权值,用于排序
输入:const struct Tuple* a, const struct Tuple* b,指向需要比较的两个边的指针
输出:两个边的权值之差
*/
int cmp(const struct Tuple* a, const struct Tuple* b) {
return a->z - b->z;
}
/*
这个函数实现了找到最小代价路径的功能
输入:int** heights,表示二维高度数组;int heightsSize,高度数组的行数;int* heightsColSize,高度数组的列数
输出:最小的代价值
*/
int minimumEffortPath(int** heights, int heightsSize, int* heightsColSize) {
int m = heightsSize;
int n = heightsColSize[0];
struct Tuple edges[n * m * 2];
int edgesSize = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int id = i * n + j;
if (i > 0) {
edges[edgesSize].x = id - n;
edges[edgesSize].y = id;
edges[edgesSize++].z = fabs(heights[i][j] - heights[i - 1][j]);
}
if (j > 0) {
edges[edgesSize].x = id - 1;
edges[edgesSize].y = id;
edges[edgesSize++].z = fabs(heights[i][j] - heights[i][j - 1]);
}
}
}
qsort(edges, edgesSize, sizeof(struct Tuple), cmp);
struct DisjointSetUnion* uf = malloc(sizeof(struct DisjointSetUnion));
initDSU(uf, m * n);
int ans = 0;
for (int i = 0; i < edgesSize; i++) {
unionSet(uf, edges[i].x, edges[i].y);
if (connected(uf, 0, m * n - 1)) {
ans = edges[i].z;
break;
}
}
return ans;
}
阅读全文