以下是老师问题具体要求
求解最小生成树的Prim加点法和Kruskal加边法代码实现:分别以邻接矩阵和邻接表数据结构来表示和存储一个图(最好同时支持无向图和有向图),分别利用Prim加点法和Kruskal加边法求解其最小生成树,共四个求解函数(邻接矩阵+Prim加点法,邻接表+Prim加点法,邻接矩阵+Kruskal加边法,邻接表+Kruskal加边法)。在此基础上,借助OpenCV画出原来的图(顶点以数据域中的三个整数数值画相应颜色的圆,边以黑色的线条来表示),以红色的线条来取代黑色的线条表示得到的最小生成树上的边
具体过程我会在代码中详细注释!
图的数据具体参考我的二维数组
#include <opencv.hpp>
#include<stdio.h>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<stack>
#include<queue>
#include<stdlib.h>
#include <iostream>
#define F 9999999
using namespace cv;
using namespace std;\\无视这么多头文件,我每次做作业直接复制这些的
Mat srcImage1(600, 1000, CV_8UC3, Scalar(255, 255, 255));\\创建个白板 600×1000的
int mape[12][12] = {
{0,5,0,1,0,0,0,0,0,0,0,0},
{5,0,6,0,0,0,8,0,0,0,0,0},
{0,6,0,6,0,0,0,1,0,0,0,0},
{1,0,6,0,0,0,3,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,9,0,0},
{0,0,0,0,0,0,0,0,0,0,4,0},
{0,8,0,3,0,0,0,0,0,7,0,0},
{0,0,1,0,0,0,0,0,0,0,0,3},
{0,0,0,0,2,0,0,0,0,0,0,0},
{0,0,0,0,9,0,7,0,0,0,3,0},
{0,0,0,0,0,4,0,0,0,3,0,4},
{0,0,0,0,0,0,0,3,0,0,4,0}\\ok 这就是我的图的数据,我接下来的邻接表直接根据这个数组存的
};
struct v {
int e;
int edist;
struct v *next;
}*V[12];
struct v *p[12];
struct linkmape {
struct v *link;
int numb;
}ML[12];\\我的ljb的定义
struct Mape {
int Mapex;
int Mapey;
int numb;
}M[12];
stack<struct Mape> S;
stack<struct linkmape> SS;
queue<struct Mape>Q;
queue<struct linkmape>QQ;\\懒得自己实现stack queue 用了c++模块,感兴趣的同学自己去学
void draw()\\预存我每个节点在x,y上的具体位置,并画下来,draw 顾名思义
{
int count = 0, y = 100, x = 200;
for (int i = 0; i < 12; i++)
{
if (count == 3)
{
y = y + 120;
count = 0;
x = 200;
}
M[i].numb = i;
M[i].Mapex = x;
M[i].Mapey = y;
count++;
x