2019独角兽企业重金招聘Python工程师标准>>>
代码:
#include
//闹了好长时间才闹好,如果有不懂的地方可以留言
//里面判断环的地方用到并查集
//并查集资料:http://dongxicheng.org/structure/union-find-set/
#define ENUM 15//边的数量
#define VNUM 9//顶点的数量
#define MV 0
typedef struct _tag_Edge //存储边的信息
{
int begin;
int end;
int weight;//权值
}Edge;
int father[VNUM]; //存储
int son[VNUM];
int Matvix[VNUM][VNUM]=
{//图
{0, 10, MV, MV, MV, 11, MV, MV, MV},
{10, 0, 18, MV, MV, MV, 16, MV, 12},
{MV, 18, 0, 22, MV, MV, MV, MV, 8},
{MV, MV, 22, 0, 20, MV, 24, 16, 21},
{MV, MV, MV, 20, 0, 26, MV, 7, MV},
{11, MV, MV, MV, 26, 0, 17, MV, MV},
{MV, 16, MV, 24, MV, 17, 0, 19, MV},
{MV, MV, MV, 16, 7, MV, 19, 0, MV},
{MV, 12, 8, 21, MV, MV, MV, MV, 0},
};
void swap(Edge array[], int i, int j)
{
Edge temp = array[i];
array[i] = array[j];
array[j] = temp;
}
void SelectionSort(Edge array[], int len) // O(n*n)
{
int i = 0;
int j = 0;
int k = -1;
for(i=0; i %d weight:%d\n", array[i].begin, array[i].end, array[i].weight);
}
if(total == VNUM-1) //最小生成树条件:边数=顶点数-1
{
flag = 1;
break;
}
}
if(flag)
printf("%d\n", sum);
else
printf("error.\n");
return 0;
}