//这段代码还没有优化,第一个就是 在kruskal方法中的iswrong可以有两个地方改进
//一个是不使用 int[][] 而使用 map
//第二个就是 加进 wrongarr时可以使用 位运算来解决 (可以按这个思路去研究,本人还没有试过)
//isok() 可以使用位 运算来解决 假设有三个点 则 000 添加一个点 001 再添加 101 ... 根据他们的位置就可以判断出来了#include <cstdlib>
#include <iostream>
#define TITLE_T cout << "###############################" << endl;
#define TITLE_I cout << " 无向图转换成最小生成树" << endl;
using namespace std;
struct Edge {
int fr;
int to;
int value;
void copy(struct Edge *p)
{
this->fr = p->fr;
this->to = p->to;
this->value = p->value;
}
};
/
class AdjacencyMatrix
{
public:
AdjacencyMatrix(int cn, int en);
~AdjacencyMatrix();
inline int setvalue(int pref, int next, int value);
int check() const;
int convert_prim(int logflag = 0);
int convert_kruskal(struct Edge *p_e, int logflag = 1);
void print_m() const;
void print_t() const;
private:
int citynum;
int edgenum;
int **matrix;
struct Edge *s_edge;
int iswrong(int ** wrongarr, struct Edge * p_e, int index); //kruskal
int isok(int *okarr, int fr, int to); //kruskal
};
AdjacencyMatrix::AdjacencyMatrix(int cn, int en)
:citynum(cn), edgenum(en)
{
int ** temp = new int*[cn];
for(int i = 0; i < cn; i++)
{
temp[i] = new int[cn];
}
for(int j = 0; j < cn; j++)
{
for(int k = 0; k < cn; k++)
{
if( j == k )
temp[j][j] = 0;
else
temp[j][k] = -1;
}
}
matrix = temp;
s_edge = NULL;
}
inline int AdjacencyMatrix::setvalue(int pref, int next, int value)
{
if( value <= 0 || pref >= citynum || next >= citynum || pref == next || *(*(matrix+pref)+next) > 0 )
return 0;
matrix[pref][next] = value;
matrix[next][pref] = value;
return value;
}
AdjacencyMatrix::~AdjacencyMatrix()
{
for(int i = 0; i < citynum; i++)
delete[] matrix[i];
delete[] matrix;
if( s_edge )
delete[] s_edge;
}
void AdjacencyMatrix::print_m() const
{
cout << "AdjacencyMatrix Info:/n";
for(int i = 0; i < citynum; i++)
{
for(int j = 0; j < citynum; j++)
cout << matrix[i][j] << "/t";
cout << endl;
}
}
void AdjacencyMatrix::print_t() const
{
if( s_edge )
{
cout << "min tree Info:/n";
for(int i = 0; i < citynum-1; i&