建立含任意结点的无向连通网用Prim算法构造其最小生成树 只能运行一部分

qq_29498333 2015-07-03 02:11:02
求大神帮忙

输入
若干行整数
第一行为两个整数,分别为图的顶点数和边数
第二行开始是该图的邻接矩阵,主对角线统一用0表示,无直接路径的两点用100来表示(保证各边权值小于100)
输出
若干用空格隔开的整数
样例输入
6 10
0 6 1 5 100 100
6 0 5 100 3 100
1 5 0 5 6 4
5 100 5 0 100 2
100 3 6 100 0 6
100 100 4 2 6 0
样例输出
1 4 2 5 3


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MaxSize 6
#define INF 100
typedef struct{
int no; //askjfkajsdf
int w;
int name;

}VertexType;
typedef struct{
int edges[MaxSize][MaxSize];
int n,e;
VertexType point[MaxSize];
}MGraph;

void CreateMGraph(MGraph &M)
{
int i,j,n;
int edges[MaxSize][MaxSize];
printf("input 边数:");
scanf("%d",&n);//nnnnn
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&edges[i][j]);
}
}
}

typedef struct node{
int adjvex;
struct node *next;
}ArcNode;
typedef struct Vnode{
int w;
int name;
ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MaxSize];
typedef struct{
AdjList adjlist;
int n,e;
}ALGraph;

void CreateALGraph(ALGraph *G,MGraph M)
{
int i,j,n=M.n;
ArcNode *p;
G=(ALGraph*)malloc(sizeof(ALGraph));
for(i=0;i<G->n;i++){
G->adjlist[i].firstarc=NULL;
}
for(i=0;i<n;i++)
{
for(j=n-1;j>=0;j--)
{
if(M.edges[i][j]!=0){
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->next=G->adjlist[i].firstarc;
G->adjlist[i].firstarc=p;
}
G->n=n;
G->e=M.e;
}
}
}
void Prim(MGraph M,int v)
{
int lowcost[MaxSize];
int min;
int closest[MaxSize],i,j,k;
for(i=0;i<M.n;i++)
{
lowcost[i]=M.edges[v][i];
closest[i]=v;
}
for(i=1;i<M.n;i++)
{
min='100';
for(j=0;j<M.n;j++)
{if(lowcost[j]!=0&&lowcost[j]<min)
{
min=lowcost[j];
k=j;
}
printf("bian(%d,%d):%d\n",closest[k],k,min);
lowcost[k]=0;
for(j=0;j<M.n;j++)
if(M.edges[k][j]!=0&&M.edges[k][j]<lowcost[j])
{
lowcost[j]=M.edges[k][j];
closest[j]=k;
}
}
}
}
int main()
{
ALGraph *G;
MGraph M;
CreateMGraph(M);

Prim(M,M.n);
return 0;
}



只能完成到输入矩阵,后面就无法输入最小生成树的序列了 还请各位大神帮忙,谢谢
...全文
358 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟花散尽13141 2016-09-01
  • 打赏
  • 举报
回复
http://download.csdn.net/detail/u013132051/9618521 源码下载

64,684

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧