问题:用有向图的邻接矩阵存储结构实现prim算法
弧的其它信息形式为vi-vj(vi为弧尾结点,vj为弧头结点),故标志位Info取1,在求最小生成树的时候输出的就是弧的信息。
#include<iostream>
#include<string>
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;i++)
{
if(!strcmp(v,G.vexs[i]))
return i;
}
return -1;
}
void CreateGraph(MGraph &G)
{
int i,j,l;
VertexType vex_t,vex_h;
int pos_t,pos_h;
InfoType Info;
VRType w;
string s;
cout<<"请输入带权有向图的顶点数,弧数,弧是否含其它信息(是:1,否:0)"<<endl;
cin>>G.vexnum>>G.arcnum>>Info;
cout<<"请以此输入"<<G.vexnum<<"个顶点的值:";
for(i=0;i<G.vexnum;i++)
{
cin>>G.vexs[i];
}
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
{
G.arcs[i][j].adj=INFINITY;
G.arcs[i][j].info=NULL;
}
}
cout<<endl;
cout<<"请依次输入"<<G.arcnum<<"条弧的弧尾,弧头,权值:"<<endl;
for(i=0;i<G.arcnum;i++)
{
cout<<"请输入第"<<i+1<<"条弧的弧尾,弧头和权值:"<<endl;
cin>>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<<"请输入第"<<i+1<<"条弧的信息:"<<endl;
cin>>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;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
{
if(G.arcs[i][j].info)
{
cout<<G.arcs[i][j].adj<<' '<<G.arcs[i][j].info<<endl;
}
}
}
}
int FindMin(closedge c,int vexnum)
{
int i;
int k=0;
VRType x=INFINITY;
for(i=0;i<vexnum;i++)
{
if(c[i].lowcost!=0)
{
if(c[i].lowcost<x)
{
x=c[i].lowcost;
k=i;
}
}
}
return k;
}
int Prim(MGraph G,int i)
{
int j,k,l;
int sum=0;
closedge c;
VertexType vex_t;
int pos_h,pos_t;
if(i<1||i>G.vexnum)
{
cout<<"输入不合法,不存在此顶点~"<<endl;
return 0;
}
strcpy(c[i-1].vex,G.vexs[i-1]);
c[i-1].lowcost=0;
for(j=0;j<G.vexnum;j++)
{
if(j!=i-1)
{
strcpy(c[j].vex,G.vexs[i-1]);
c[j].lowcost=G.arcs[i-1][j].adj;
}
}
cout<<"构成最小生成树的弧有:"<<endl;
for(j=0;j<G.vexnum-1;j++)
{
k=FindMin(c,G.vexnum);
strcpy(vex_t,c[k].vex);
pos_t=LocateVex(G,vex_t);
pos_h=k;
c[k].lowcost=0;
cout<<G.arcs[pos_t][pos_h].info<<' '<<G.arcs[pos_t][pos_h].adj<<endl;
sum+=G.arcs[pos_t][pos_h].adj;
for(l=0;l<G.vexnum;l++)
{
if(c[l].lowcost!=0)
{
if(c[l].lowcost>G.arcs[k][l].adj)
{
c[l].lowcost=G.arcs[k][l].adj;
strcpy(c[l].vex,G.vexs[k]);
}
}
}
}
cout<<"最小生成树的最短路径为:"<<sum<<endl;
return 1;
}
void main()
{
int i;
MGraph G;
CreateGraph(G);
cout<<"请输入初始出发点:";
cin>>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!
-------------------------------------------------