65,187
社区成员




#ifndef MGRAPH_H_INCLUDED
#define MGRAPH_H_INCLUDED
template <class T>
class Graph
{
public:
virtual int Insert(int u, int v, T& w) = 0;
virtual int Remove(int u, int v) = 0;
virtual bool Exist(int u, int v) const = 0;
protected:
int n; // 顶点数
int e; // 边数
};
template <class T>
class MGraph : public Graph<T>
{
public:
MGraph(int mSize, const T& noedg);
~MGraph();
int Insert(int u, int v, T& w);
int Remove(int u, int v);
bool Exist(int u, int v) const;
protected:
T ** a;
T ** noEdge;
};
template <class T>
MGraph<T>::MGraph(int mSize, const T& noedg)
{
n = mSize;
e = 0;
noEdge = noedg;
a = new T* [n];
for (int i = 0; i < n; i++)
{
a[i] = new T[n];
for (int j = 0; j < n; j++)
{
a[i][j] = noEdge;
}
a[i][i] = 0;
}
}
template <class T>
MGraph<T>::~MGrpah()
{
for (int i = 0; i < n; i++)
{
delete [] a[i];
delete [] a;
}
}
template <class T>
bool MGraph<T>::Exist(int u, int v) const
{
if ( u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v || a[u][v] == noEdge)
return false;
return true;
}
template <class T>
int MGraph<T>::Insert(int u, int v, T& w)
{
if ( u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v )
return -1;
if (a[u][v] != noEdge)
return 0;
a[u][v] = w;
e ++;
return 1;
}
template <class T>
int MGraph<T>::Remove(int u, int v)
{
if ( u < 0 || v < 0 || u > n - 1 || v > n - 1 || u == v )
return -1;
if (a[u][v] == noEdge)
return 0;
a[u][v] = noEdge;
e --;
return 1;
}
#endif // MGRAPH_H_INCLUDED