刚学数据结构,在图中找最断路径,请大家帮帮忙!!
SeqList.h文件
/****************************************************************************/
/*功能说明:顺序表,用于存放图的顶点 */
/*类名:SeqList */
/*中文名:顺序表 */
/*主要函数:构造函数SeqList(int MaxSize=defaultSize)
定位函数int Find(Type &x);找出x在表中的位置
插入数据函数int insert(Type &x,int i),
其中i表示插入的位置
*/
/*成员变量: */
/****************************************************************************/
/*****************类定义*****************/
template <class Type> class SeqList{
// friend class Graph;
public:
SeqList(int MaxSize=defaultSize);
~SeqList(){
delete []data;}
int Find(Type &x)const;
int InSert(Type &x,int i);
private:
Type *data;
int MaxSize;
int last;
};
/****************************************************************************/
/****************************************************************************/
/***********成员函数定义****************/
template<class Type> SeqList<Type>::SeqList(int sz){
if(sz>0){
MaxSize=sz;last=-1;
data=new Type[MaxSize];
}
}
template<class Type> int SeqList<Type>::Find(Type &x)const{
int i=0;
while(i<=last&&data[i]!=x)i++;
if (i>last)return -1;
else return i;
}
template<class Type> int SeqList<Type>::InSert(Type &x,int i){
if(i<0||i>last+1||last==MaxSize-1)return 0;
else{
last++;
for(int j=last;j>i;j--)
data[j]=data[j-1];
data[i]=x;
return 1;
}
}
/*************************************结束************************************/
Graph.h文件
/****************************************************************************/
/*功能说明: 图的实现 */
/*类名: Graph */
/*中文名: 图 */
/*成员函数: */
/*成员变量: */
/****************************************************************************/
#include"SeqList.h"
/*************全局变量定义********************/
//最大边数
const int MaxNurnEdges = 50;
//最大顶点数
const int MaxNurnVertices=10;
//最大权值
const int MaxW = 1000;
const int MAXNUM=1000;
/**************************图的类定义**************************/
//图的类定义
//NameType为边权值的类型, DistType顶点的类型
template<class NameType, class DistType> class Graph{
private:
//顶点表
SeqList<DistType> VerticesList (MaxNurnVertices);
//邻接矩阵
DistType Edge[MaxNurnVertices][MaxNurnVertices] ;
//当前边数
int CurrentEdges ;
//存放从顶点0到其它各顶点的最短路径长度
DistType dist[MaxNurnVertices] ;
//存放在最短路径上该顶点的前一顶点的顶点号
NameType path[MaxNurnVertices] ;
//已求得的在最短路径上该顶点的前一顶点的顶点号
NameType S[MaxNurnVertices] ;
public:
//构造函数
Graph( int sz= MaxNumEdgesVertices);
//返回当前顶点数
int NimberOfVertices ( ){ return VerticesList. last + 1; }
//给出边(v1,v2)的权值
DistType GetWeight (int v1, int v2) ;
//设置(vl,v2)边上的权值为weight
void SetsertEdge (int v1,int v2,DistType weight);
//求最短路径函数
NameType ShortestPath (int, int,int);
int choose (int);
/*****************************************************************/
/********************************成员函数定义************************************/
//构造函数,sz为顶点数。
template<class NameType, class DistType> Graph<NameType, DistType>::Graph ( int sz ){
//邻接矩阵初始化
for(int i = 0; i<sz; i++)
//图中当前边数初始化
for(int j = 0; j<sz ; j++) Edge[ i][j] = 0;
CurrentEdges = 0;
}
//给出以顶点v1和v2为两端点的边上的权值。
template<class NameType, class DistType>
DistType Graph<NameType, DistType> ::GetWeight (int v1,int v2){
if ( v1 != -1 && v2 != -1 ) return Edge[v1][v2];
//带权图中权值为0,表示无此边
else return 0;
}
//设置(vl,v2)边上的权值为weight
template<class NameType, class DistType>
void Graph<NameType, DistType>::SetsertEdge (int v1,int v2,DistType weight){
Edge[v1][v2]=weight;
}
//求顶点v到其它顶点的最短路径
template<class NameType, class DistType>
NameType Graph<NameType, DistType>::ShortestPath (int n, int v,int t){
//dist和path数组初始化
for (int i=0; i<n; i ++){
//复制邻接矩阵第v行元素到dist中
dist[i] = Edge[v][i];
//已求出最短路径的顶点集合为空
S[i] = 0;
//有直接路径的前驱为v
if(i != v && dist[i]< MAXNUM) path[i] = v;
//无直接路径的前驱为-1
else path[i] = -1;
}
//顶点v加人顶点集合
S[v]=1; dist[v] = 0;
//从顶点v确定到i的n-1条最短路径
for (i=0; i<n-1; i++){
int min = MAXNUM; int u = v;
//选择当前不在集合S中具有最短路径的顶点u
for (int j=0; j< n; j++)
if(!S[j] && dist [j]< min) {u = j; min = dist [j];}
//u加人集合S,表示到它的最短路径已求出
S[u] = 1;
//检查经u中转到所有顶点w的路径
for ( int w = 0; w< n; w++)
//若从v经u到w的路径长短于从v到w的原路径长
if(!S[w]&&Edge[u][w]<MAXNUM&&dist[u]+Edge[u][w]< dist [w]){
//修改v到w的最短路径长
dist[w] = dist[u]+Edge[u][w];
//修改v到w的最短路径上的前驱结点为u
path[w] = u;
}
}
return dist[t];
}
/*********************************结束************************************/
主函数
#include "stdafx.h"
#include<iostream.h>
//#include"SeqList.h"
#include"Graph.h"
int main(int argc, char* argv[])
{
int b=3;
Graph<int,int> graph(b);
graph.GetWeight (0, 1);
graph.SetsertEdge(0,1,7);
graph.SetsertEdge(1,2,5);
graph.SetsertEdge(0,2,13);
cout<<graph.ShortestPath (3,0,2)<<endl;
printf("Hello World!\n");
return 0;
}
/*********************************结束************************************/
出错地方:
f:\c++实验\schchart\graph.h(40) : error C2061: syntax error : identifier 'MaxNurnVertices'
f:\c++实验\schchart\graph.h(130) : see reference to class template instantiation 'Graph<NameType,DistType>' being compiled
f:\c++实验\schchart\graph.h(40) : error C2061: syntax error : identifier 'MaxNurnVertices'
F:\C++实验\SchChart\SchChart.cpp(21) : see reference to class template instantiation 'Graph<int,int>' being compiled
执行 cl.exe 时出错.
我试了一下,把那个 SeqList<DistType> VerticesList (MaxNurnVertices)注释掉就没错,或把它后面的常量MaxNurnVertices去掉也不会出错,如果保留这一句SeqList<DistType> VerticesList (MaxNurnVertices)要怎么改。
我还是个学生,请大家多多指导,谢谢咯!!