#include<stdio.h>
#include<stdlib.h>
#define M 20
#define MAX 20
typedef struct
{
int begin;
int end;
int weight;
}edge;
typedef struct
{
int adj;
int weight;
}AdjMatrix [MAX][MAX];
typedef struct
{
AdjMatrix arc;
int vexnum,arcnum;
}MGraph;
void CreatGraph(MGraph*);
void sort(edge*,MGraph*);
void MiniSpanTree(MGraph*);
int Find(int*,int);
void Swapn(edge*,int,int);
void CreatGraph(MGraph*G)//构造图
{
int i,j,n,m;
printf("请输入城市数及边数:");
scanf("%d%d",&G->vexnum,&G->arcnum);
for(i=1;i<=G->vexnum;i++)//初始化图
{
for(j=1;j<=G->vexnum;j++)
{
G->arc[i][j].adj=G->arc[j][i].adj=0;
}
}
printf("请输入有道路连通的2个城市及他们之间的造价费用(城市1 城市2 费 用):\n");
for(i=1;i<=G->arcnum;i++)//输入边和费用
{
scanf("%d%d",&n,&m);
G->arc[n][m].adj=G->arc[m][n].adj=1;
scanf("%d",&G->arc[n][m].weight);
}
printf("邻接矩阵为:\n");
for(i=1;i<=G->vexnum;i++)
{
for(j=1;j<=G->vexnum;j++)
{
if(G->arc[i][j].adj==1)
printf("%d ",G->arc[i][j].weight);
else
printf("%d ",G->arc[i][j].adj);
G->arc[j][i].weight=G->arc[i][j].weight;
}
printf("\n");
}
}
void sort(edge edges[],MGraph *G)//对权值进行排序
{
int i,j;
for(i=1;i<G->arcnum;i++)
{
for(j=i+1;j<=G->arcnum;j++)
{
if(edges[i].weight>edges[j].weight)
{
Swapn(edges,i,j);
}
}
}
printf("按造价排序之后的边顺序为(序号 边 费用):\n");
for(i=1;i<=G->arcnum;i++)
{
printf("%d. <%d,%d> %d\n",i,edges[i].begin,edges[i].end,edges[i].weight);
}
}
void Swapn(edge*edges,int i,int j)
{
int temp;
temp=edges[i].begin;
edges[i].begin=edges[j].begin;
edges[j].begin=temp;
temp=edges[i].end;
edges[i].end=edges[j].end;
edges[j].end=temp;
temp=edges[i].weight;
edges[i].weight=edges[j].weight;
edges[j].weight=temp;
}
void MiniSpanTree(MGraph *G)//生成最小生成树
{
int i,j,n,m,Mincost=0;
int k=1;
int parent[M];
edge edges[M];
for(i=1;i<G->vexnum;i++)
{
for(j=i+1;j<=G->vexnum;j++)
{
if(G->arc[i][j].adj==1)
{
edges[k].begin=i;
edges[k].end=j;
edges[k].weight=G->arc[i][j].weight;
k++;
}
}
}
sort(edges,G);
for(i=1;i<=G->arcnum;i++)
{
parent[i]=0;
}
printf("最小生成树为:\n");
for(i=1;i<=G->arcnum;i++)//核心部分
{
n=Find(parent,edges[i].begin);
m=Find(parent,edges[i].end);
if(n!=m) //如果m、n不等说明此边没有与现有的生成树形成环路
{
parent[n]=m; //数组下标记录了边起点,数组值记录了边终点
printf("<%d,%d> %d\n",edges[i].begin,edges[i].end,edges[i].weight);
Mincost+=edges[i].weight;
}
}
printf("使各城市间能够通信的最小费用为:Mincost=%d\n",Mincost);
}
int Find(int*parent,int f) //查找从f这个坐标开始能走到的最远的结点是哪里
{
while(parent[f]>0) //如果最后走到的最远的结点相同,说明是有环路生成的
{
f=parent[f];
}
return f;
}
void main()//主函数
{
MGraph*G;
G=(MGraph*)malloc(sizeof(MGraph));
CreatGraph(G);
MiniSpanTree(G);
system("pause");
}