图求最小生成树的程序
功能良好
性能较好
源代码在线查看: 图求最小生成树.txt
图求最小生成树
#include
#include
#include
#include
static int vertexcount= 0;
static int edgecount =0;
struct VERTEX
{
char vertex[3];
bool key;
};
static VERTEX * Ve=NULL;
struct EDGE
{
char edge[3];
int GRI;
bool key;
VERTEX * Vertex1;
VERTEX * Vertex2;
};
static EDGE *Ed=NULL;
VERTEX * GetVertex(char vertex[])
{
for(int i=1;i {
if(strcmp(vertex,Ve[i-1].vertex)==0) return (Ve+i-1);
}
}
void BuildGraph()
{
char vertex[3];
cout cin>>vertexcount;
Ve=new VERTEX[vertexcount];
cout for(int i=1;i {
cin>>Ve[i-1].vertex;Ve[i-1].key=false;
}
cout cout cin>>edgecount;
Ed=new EDGE[edgecount];
for(i=1;i {
cout cin>>Ed[i-1].edge;cin>>Ed[i-1].GRI;
cin>>vertex;
Ed[i-1].Vertex1=GetVertex(vertex);
cin>>vertex;
Ed[i-1].Vertex2=GetVertex(vertex);
}
cout }
EDGE * GetMin()
{
EDGE * d=Ed;EDGE * ad=NULL;
for(int i=1;i {
if(d->key&&(d->Vertex1->key||d->Vertex2->key))
{
if(!ad) ad=d;
if(ad->GRI>d->GRI) ad=d;
}
d++;
}
ad->key=false;
ad->Vertex1->key?ad->Vertex2->key=true:ad->Vertex1->key=true;
return ad;
}
void GetTree()
{
VERTEX * p;
EDGE * d;
char vertex[3];
cout cin>>vertex;
p=GetVertex(vertex);
p->key=true;
cout for(int i=1;i {
d=GetMin();
cout }
delete []Ve;
delete []Ed;
}
int main()
{
BuildGraph();GetTree();
return 0;
}