用有向图的邻接矩阵存储结构实现prim算法

问题:用有向图的邻接矩阵存储结构实现prim算法

弧的其它信息形式为vi-vj(vi为弧尾结点,vj为弧头结点),故标志位Info取1,在求最小生成树的时候输出的就是弧的信息。

#include
#include
using namespace std;

#define MAX_VER_NUM 10    //顶点最大个数
#define INFINITY INT_MAX  //最大权值
#define MAX_NAME 5        //顶点字符串的最大长度

typedef int VRType;       //权值类型
typedef char InfoType;    
typedef char VertexType[MAX_NAME]; 

typedef struct
{
	VRType adj;       //权值
	InfoType *info;   //与弧相关信息的指针
}ArcCell,AdjMatrix[MAX_VER_NUM][MAX_VER_NUM];

typedef struct MGraph
{
	int vexnum;      //顶点数
	int arcnum;      //弧数
	AdjMatrix arcs;  //邻接矩阵
	VertexType vexs[MAX_VER_NUM]; //顶点矩阵
}MGraph;

typedef struct 
{
	VertexType vex;
	VRType lowcost;
}closedge[MAX_VER_NUM];

int LocateVex(MGraph G,VertexType v)
{
	int i;
	for(i=0;i>G.vexnum>>G.arcnum>>Info;
	cout<<"请以此输入"<>G.vexs[i];
	}
	for(i=0;i>vex_t>>vex_h>>w;
		pos_t=LocateVex(G,vex_t);
		pos_h=LocateVex(G,vex_h);
		G.arcs[pos_t][pos_h].adj=w;
		if(Info)
		{
			cout<<"请输入第"<>s;
			l=s.length();
			G.arcs[pos_t][pos_h].info=new char[l+1];
			strcpy(G.arcs[pos_t][pos_h].info,s.c_str());
		}
	}
	for(i=0;iG.vexnum)
	{
		cout<<"输入不合法,不存在此顶点~"<G.arcs[k][l].adj)
				{
					c[l].lowcost=G.arcs[k][l].adj;
					strcpy(c[l].vex,G.vexs[k]);
				}
			}
		}
	}
	cout<<"最小生成树的最短路径为:"<>i;
	Prim(G,i);	
}



----------------------------------------------------------------------------------------------------------------

Input:

6 8 1

v1 v2 v3 v4 v5 v6

v1 v2 2

v1-v2

v1 v3 1

v1-v3

v2 v5 3

v2-v5

v3 v6 5

v3-v6

v4 v1 4

v4-v1

v5 v3 3

v5-v3

v5 v6 6

v5-v6

v6 v4 7

v6-v4

1

output:

v1-v3   1

v1-v2   2

v2-v5   3

v3-v6   5

v6-v4   7

18
----------------------------------------------------------------------------------------------------------------

-------------------------------------------------

To_make_progress,everyday!

-------------------------------------------------

你可能感兴趣的:(数据结构)