#include using namespace std;
typedef char VertexType;
typedef struct EdgeNode{
int adjvex;
int weight;
struct EdgeNode *nextedge;
}EdgeNode;
typedef struct VNode{
VertexType data;
int visited;
struct EdgeNode *firstedge;
}VNode;
struct ALGraphStruct;
typedef struct ALGraphStruct *ALGraph;
struct ALGraphStruct{
VNode vexs[1000];
int vertexnum;
int edgenum;
};
//创建邻接表
ALGraph CreateALGraph()
{
ALGraph g=(ALGraph)malloc(sizeof(struct ALGraphStruct));
g->vertexnum=0;
g->edgenum=0;
return g;
}
//查找顶点位置
int Locates(ALGraph g,VertexType v)
{
for(int i=0;ivertexnum;i++)
if(g->vexs[i].data==v)
return i;
return -1;
}
//增加顶点
void AddVertex(ALGraph g,VertexType v)
{
int k=Locates(g,v);
if(k!=-1)
return ;
int n=g->vertexnum;
g->vertexnum++;
g->vexs[n].data=v;
g->vexs[n].visited=0;
g->vexs[n].firstedge=NULL;
}
//查找边
EdgeNode *FindEdge(ALGraph g,int i,int j)
{
for(EdgeNode *p=g->vexs[i].firstedge;p!=NULL;p=p->nextedge)
if(p->adjvex==j)
return p;
return NULL;
}
//插入边
void InsertEdge(ALGraph g,int i,int j,int w)
{
EdgeNode *s=(EdgeNode*)malloc(sizeof(struct EdgeNode));
s->adjvex=i;
s->weight=w;
s->nextedge=g->vexs[j].firstedge;
g->vexs[j].firstedge=s;
}
//增加边
void AddEdge(ALGraph g,VertexType v1,VertexType v2,int w)
{
EdgeNode *s,*t;
int i=Locates(g,v1);
int j=Locates(g,v2);
if(i==-1 || j==-1)
return ;
s=FindEdge(g,i,j);
if(s!=NULL){
t=FindEdge(g,j,i);
if(s->weight>w){
s->weight=w;
t->weight=w;
}
return;
}
InsertEdge(g,i,j,w);
InsertEdge(g,j,i,w);
g->edgenum++;
}
//第一个邻接点
int FirstEdge(ALGraph g,int i)
{
EdgeNode *p=g->vexs[i].firstedge;
if(p!=NULL)
return p->adjvex;
return -1;
}
//下一个邻接点
int NextEdge(ALGraph g,int i,int j)
{
EdgeNode *p=FindEdge(g,i,j);
if(p==NULL)
return -1;
p=p->nextedge;
if(p==NULL)
return -1;
return p->adjvex;
}
//邻接表的初始化
void ALGraphInit(ALGraph g)
{
int n,m,w;
char v,x,y;
scanf("%d%d",&n,&m);
getchar();
for(int i=0;iv=getchar();
AddVertex(g,v);
}
for(int j=0;jgetchar();
x=getchar();
y=getchar();
w=getchar();
AddEdge(g,x,y,w);
}
for(int k=0;kg->vexs[k].visited=1;
}
//最小生成树之prime算法
typedef struct CloseEdge{
int adjvex;
int weight;
}CloseEdge;
int FindMin(CloseEdge closedge[],int n)
{
int k=-1;
int min=INT_MAX;
for(int i=0;iif(closedge[i].weight==0)
continue;
if(closedge[i].weightk=i;
min=closedge[i].weight;
}
}
return k;
}
int Prime(ALGraph g,ALGraph t,VertexType v)
{
int lowcost=0;
int k=Locates(g,v);
if(k==-1)
return -1;
CloseEdge closedge[1000];
for(int i=0;ivertexnum;i++)
closedge[i].weight=INT_MAX;
closedge[k].weight=0;
AddVertex(t,g->vexs[k].data);
for(EdgeNode *p=g->vexs[k].firstedge;p!=NULL;p=p->nextedge){
closedge[p->adjvex].adjvex=k;
closedge[p->adjvex].weight=p->weight;
}
for(int i=0;ivertexnum;i++){
k=FindMin(closedge,g->vertexnum);
if(k==-1)
return -1;
int j=closedge[k].adjvex;
AddVertex(t,g->vexs[k].data);
AddEdge(t,g->vexs[j].data,g->vexs[k].data,closedge[k].weight);
lowcost+=closedge[k].weight;
closedge[k].weight=0;
for(EdgeNode *p=g->vexs[k].firstedge;p!=NULL;p=p->nextedge){
if(closedge[p->adjvex].weight==0)
continue;
if(closedge[p->adjvex].weight>p->weight){
closedge[p->adjvex].weight=p->weight;
closedge[p->adjvex].adjvex=k;
}
}
}
return lowcost;
}
int main()
{
return 0;
}