#include <iostream>
#include <queue>
using namespace std;
#define MAX_VERTEX_NUM 20
typedef struct ArcNode {
int adjvex;//该点在头结点表里的编号
struct ArcNode* nextarc;
int weight;//权值(和对应头结点的距离)
}ArcNode;
typedef struct VNode {//Vertex 头结点
//int data; //头结点的数据
ArcNode* firstarc;//与头结点相连第一个弧结点(表结点)
string name;
int enter;
}VNode, AdjList[MAX_VERTEX_NUM];//VNode=struct VNode
typedef struct {
AdjList vertices;//是个数组
int vexnum;//点数
int arcnum;
}ALGraph;
//v1 A v2 B 返回AB在表中的下标
int LocateVex(ALGraph& temp, string tempname) {
int a = -1;
for (int i = 0; i < temp.vexnum; ++i) {
if (temp.vertices[i].name == tempname) {
a = i;
}
}
return a;
}
void CreateAdjList(ALGraph& G) {
//根据输入的有向图G的顶点数及边数,建立图G的邻接表
cout << "输入点数" << endl;
cin >> G.vexnum;
cout << "输入边数" << endl;
cin >> G.arcnum;
for (int i = 0; i < G.vexnum; ++i) { //初始化头结点
cout << "输入第" << i + 1 << "个结点的名字" << endl;
cin >> G.vertices[i].name;
//cout << "输入第" << i + 1 << "个结点的数据" << endl;
//cin >> G.vertices[i].data;
G.vertices[i].enter = 0;//0意味着没有加入最小生成树中
G.vertices[i].firstarc = NULL;
}
string v1, v2;
int length;
for (int k = 0; k < G.arcnum; ++k) {//构造邻接表
cout << "输入边的两端(名字)" << endl;
cin >> v1 >> v2;//输入一条边
cout << "输入边的长度" << endl;
cin >> length;
int i = LocateVex(G, v1);
int j = LocateVex(G, v2); //确定v1和v2在G中的位置
ArcNode* s = new ArcNode;
s->adjvex = j;
s->weight = length;
s->nextarc = NULL;
ArcNode* p = new ArcNode;//一个中间变量
p = G.vertices[i].firstarc;
if (!p) {
G.vertices[i].firstarc = s;
}
else {
while (p->nextarc) {
p = p->nextarc;
}
p->nextarc = s;
}//这是一个方向
ArcNode* m = new ArcNode;
m->adjvex = j;
m->weight = length;
m->nextarc = NULL;
ArcNode* n = new ArcNode;//一个中间变量
n = G.vertices[i].firstarc;
if (!n) {
G.vertices[i].firstarc = m;
}
else {
while (n->nextarc) {
n = n->nextarc;
}
n->nextarc = m;
}//另一个方向
}
}//CreateAdjList
struct Compare {
bool operator()(ArcNode* a, ArcNode* b) {
return a->weight > b->weight;
}
};
ArcNode* find_minbranch(ALGraph& G, int finish[MAX_VERTEX_NUM], int finishnum) {//finish[]是已经加入最小生成树的结点,数据是该结点在AdjList里的下标,finishnum是数组里已经有的结点个数-1
ArcNode* temp;
priority_queue<ArcNode*, vector<ArcNode*>, Compare>minbranch;//优先队列(类型 容器 比较函数)然后名字
for (int i = 0; i < finishnum; i++) {
temp = G.vertices[finish[i]].firstarc;
while (temp) {
if (G.vertices[temp->adjvex].enter) {
temp = temp->nextarc;
}
else {
minbranch.push(temp);
temp = temp->nextarc;
}
}
}
return minbranch.top();
}
void CreateMST(ALGraph& G) {
int finishnum=1;
int* finish = new int[MAX_VERTEX_NUM];//进入的名单
finish[0] = 0;//先让第一个点进去
G.vertices[finish[0]].enter = 1;
for (int j = 1; j < G.vexnum; j++) {
finish[j] = find_minbranch(G, finish, finishnum)->adjvex;
G.vertices[finish[j]].enter = 1;
int n = 0;
int judge=1;
ArcNode* temp;
temp = G.vertices[finish[j]].firstarc;
for (int m = 1; m < G.vexnum && judge; m++) {
if (temp->weight == find_minbranch(G, finish, finishnum)->weight) {
for (int h = 0; h < finishnum; h++) {
if (temp->adjvex == finish[h]) {
n = finish[h];
judge = 0;
break;
}
else{
temp = temp->nextarc;
}
}
}
else {
temp = temp->nextarc;
}
}//这一堆用来找前面那个点*/
cout<< G.vertices[n].name << "--" << G.vertices[finish[j]].name << endl;
finishnum++;
}
}
int main() {
ALGraph G;
CreateAdjList(G);
CreateMST(G);
return 0;
}
然后我又修改了一版。。。