小编Ros*_*osh的帖子

qsort 没有正确排序内容,kruskal 算法

我正在尝试使用 对结构数组进行排序qsort,但它没有正确对内容进行排序。结构节点由起始顶点、结束顶点以及从顶点“a”到达顶点“b”的成本组成。

我正在编写克鲁斯卡尔算法的代码

#include <stdio.h>
#include <stdlib.h>

int v, e;

typedef struct node {
    int a;
    int b;
    int cost;
} node;

int compare(const void *a, const void *b) {
    const node *x = *(node **)a;
    const node *y = *(node **)b;
    return (x->cost > y->cost) ? 1 : 0;
}

int main() {
    scanf("%d %d", &v, &e);
    int i;
    node *arr[e];
    for (i = 0; i < e; i++) {
        int a, b, cost;
        scanf("%d %d %d", …
Run Code Online (Sandbox Code Playgroud)

c sorting struct qsort kruskals-algorithm

2
推荐指数
1
解决办法
62
查看次数

标签 统计

c ×1

kruskals-algorithm ×1

qsort ×1

sorting ×1

struct ×1